Home » Java » Zip and Unzip the Folders and Files Using Java

Zip and Unzip the Folders and Files Using Java

Posted by: admin December 28, 2021 Leave a comment

Questions:

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?

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);
      }
    }
  }
}

###

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.

###

You can use ZIP file handling library that comes with JDK
Also this tutorials might be helpful.

###

Java have a java.util.zip.ZipInputStream and along with this you can use ZipEntry … Something like

public static void unZipIt(String zipFile, String outputFolder){
File folder = new File(zipFile);
    List<String> files = listFilesForFolder(folder);
    System.out.println("Size " + files.size());
    byte[] buffer = new byte[1024];
    try{
    Iterator<String> iter = files.iterator();
    while(iter.hasNext()){
        String file = iter.next();
    System.out.println("file name " + file);    
    ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
    ZipEntry ze = zis.getNextEntry();
    while(ze!=null){
           String fileName = ze.getName();
          File newFile = new File(outputFolder + File.separator + fileName);
          System.out.println("file unzip : "+ newFile.getAbsoluteFile());
           new File(newFile.getParent()).mkdirs();
           FileOutputStream fos = new FileOutputStream(newFile);             
           int len;
           while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
           }
           fos.close();   
           ze = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
    System.out.println("Done");
    }
   }catch(IOException ex){
      ex.printStackTrace(); 
   }
  }