Compress files using the Java ZIP API : File Commands : File Input Output : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  File Input Output   » [  File Commands  ]  Screenshots 
 



Compress files using the Java ZIP API

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Compress {
  public static void gzipFile(String from, String tothrows IOException {
    FileInputStream in = new FileInputStream(from);
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
      out.write(buffer, 0, bytesRead);
    in.close();
    out.close();
  }

  /** Zip the contents of the directory, and save it in the zipfile */
  public static void zipDirectory(String dir, String zipfile)
      throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
      throw new IllegalArgumentException("Not a directory:  "
          + dir);
    String[] entries = d.list();
    byte[] buffer = new byte[4096]// Create a buffer for copying
    int bytesRead;

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    for (int i = 0; i < entries.length; i++) {
      File f = new File(d, entries[i]);
      if (f.isDirectory())
        continue;//Ignore directory
      FileInputStream in = new FileInputStream(f)// Stream to read file
      ZipEntry entry = new ZipEntry(f.getPath())// Make a ZipEntry
      out.putNextEntry(entry)// Store entry
      while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
      in.close()
    }
    out.close();
  }

  public static void main(String args[]) throws IOException {
    String from ".";
    File f = new File(from);
    boolean directory = f.isDirectory()// Is it a file or directory?

    Compress.zipDirectory(from, from ".zip");
    Compress.gzipFile(from, from ".gz");
  }
}

           
       
Related examples in the same category
1.  File Copy in Java
2.  Counts words in a file, outputs results in sorted form
3.  Copying a file using channels and buffers
4.  Copy files using Java IO API Copy files using Java IO API
5.  Mimic the Unix Grep command
6.  File concatenation
7.  Delete file using Java IO API
8.  Undent - remove leading spaces
9.  TeePrintStream tees all PrintStream operations into a file, rather like the UNIX tee(1) command
10.  Delete a file from within Java, with error handling
11.  DirTree - directory lister, like UNIX ls or DOS and VMS dir DirTree - directory lister, like UNIX ls or DOS and VMS dir
12.  Program to empty a directory
13.  Report on a file's status in Java
14.  Simple directory lister
15.  Readonly Files
16.  List root directory List root directory
17.  Rename a file in Java
18.  FNFilter - directory lister using FilenameFilter
19.  mkdir examples
20.  Program to remove files matching a name in a directory
21.  Ls directory lister modified to use FilenameFilter Ls directory lister modified to use FilenameFilter
22.  Dir Stack Dir Stack
23.  Word Count
24.  Diff: text file difference utility. Diff: text file difference utility.
25.  Count chars in a File
























Home| Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.