Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip and Unzip the Folders and Files Using Java

Tags:

java

zip

gzip

unzip

If My Application wants to zip the Resultant files(group of Files) using java in a dynamic way, what are the available options in Java? When i Browsed I have got java.util.zip package to use, but is there any other way where I can use it to implement?

like image 907
gmhk Avatar asked Feb 16 '10 09:02

gmhk


People also ask

How do I unzip a zip file in Java?

To unzip a zip file, we need to read the zip file with ZipInputStream and then read all the ZipEntry one by one. Then use FileOutputStream to write them to file system. We also need to create the output directory if it doesn't exists and any nested directories present in the zip file.

What is zipping and unzipping in Java?

ZIP is a common file format that compresses one or more files into a single location. It reduces the file size and makes it easier to transport or store. A recipient can unzip (or extract) a ZIP file after transport and use the file in the original format.

How can I ZIP a file in Java?

Creating a zip archive for a single file is very easy, we need to create a ZipOutputStream object from the FileOutputStream object of destination file. Then we add a new ZipEntry to the ZipOutputStream and use FileInputStream to read the source file to ZipOutputStream object.

What is ZipInputStream in Java?

This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.


2 Answers

public class FolderZiper {
  public static void main(String[] a) throws Exception {
    zipFolder("c:\\a", "c:\\a.zip");
  }

  static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
  }

  static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
      throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
      addFolderToZip(path, srcFile, zip);
    } else {
      byte[] buf = new byte[1024];
      int len;
      FileInputStream in = new FileInputStream(srcFile);
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
      while ((len = in.read(buf)) > 0) {
        zip.write(buf, 0, len);
      }
    }
  }

  static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
      throws Exception {
    File folder = new File(srcFolder);

    for (String fileName : folder.list()) {
      if (path.equals("")) {
        addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
      } else {
        addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
      }
    }
  }
}
like image 61
Vicky Avatar answered Sep 19 '22 00:09

Vicky


The original Java implementation is known to have some bugs related to files encoding. For example it can't properly handle filenames with umlauts.

TrueZIP is an alternative that we've used in our project: https://truezip.dev.java.net/ Check the documentation on the site.

like image 42
Daniel Bleisteiner Avatar answered Sep 22 '22 00:09

Daniel Bleisteiner