How to Create zip archive in Java?

Here is a some example to explain how to create zip archive in Java, here more information about compression with java.

  • Firsth simple example File compression:
package com.nourit.javaIO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileCompression {

  public static void main(String[] args) throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("IT Training Center for Java and Web technology...");

    File file = new File("D:/tmp/myZipFile.zip");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
    //Get name for Zip Entry 
    ZipEntry zipEntry = new ZipEntry("myNouritFile.txt");
    out.putNextEntry(zipEntry);
    //write a content in myNouritFile.txt
    byte[] data = sb.toString().getBytes();
    out.write(data, 0, data.length);
    out.closeEntry();
    out.close();
    System.out.println("Operation successful!");
  }

}
  • Second simple example File compression:

make the content from NOUR-IT.txt in another File myNouritFile.txt and compressed.

package com.nourit.javaIO;

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

import org.apache.commons.io.FileUtils;

public class FileCompression1 {

  public static void main(String[] args) throws IOException {

    File file = new File("D:/tmp/myZipFile.zip");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
    //Get name for Zip Entry 
    ZipEntry zipEntry = new ZipEntry("myNouritFile.txt");
    out.putNextEntry(zipEntry);
    //make the content from NOUR-IT.txt in myNouritFile.txt and compressed
    File myfile = new File("D:/tmp/NOUR-IT.txt");
    byte[] data = FileUtils.readFileToByteArray(myfile);
    out.write(data, 0, data.length);
    out.closeEntry();
    out.close();
    System.out.println("Operation successful!");
  }
}
  • Thirth example Folder compression:

Here is an example to explain how to zip all files from folder in java.

package com.nourit.javaIO;

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

import org.apache.commons.io.FileUtils;

public class FileCompression2 {

  private static final String ZIP_OUTPUT_FILE = "D:\\tmp\\NOURITFile.zip";
  private static final String SOURCE_FOLDER = "D:\\tmp";
  
  public static void main(String[] args) throws IOException {
    File source_folder = new File(SOURCE_FOLDER);
    File[] files = source_folder.listFiles();
    FileOutputStream fileOutputStream = new FileOutputStream(ZIP_OUTPUT_FILE);
    ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
    writeListFilesInZipOutputStream(source_folder, files, zipOutputStream);
    zipOutputStream.closeEntry();
    zipOutputStream.close();
    System.out.println(ZIP_OUTPUT_FILE+" Created, Operation successful!");

  }

  static public void writeListFilesInZipOutputStream(File path, File[] files, ZipOutputStream zipOutputStream) {
    if (path.exists()) {
      for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
          File[] children = files[i].listFiles();
          writeListFilesInZipOutputStream(files[i], children, zipOutputStream);
        }else {
          System.out.println("File in ZIP File : " + files[i].getAbsolutePath());
          String file = files[i].getAbsolutePath().substring(SOURCE_FOLDER.length() + 1);
          ZipEntry zipEntry = new ZipEntry(file);
          try {
            zipOutputStream.putNextEntry(zipEntry);
            byte[] data = FileUtils.readFileToByteArray(files[i]);
            zipOutputStream.write(data, 0, data.length);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
}

Output:

File in ZIP File : D:\tmp\NOUR-IT-Destination.txt
File in ZIP File : D:\tmp\NOUR-IT.txt
File in ZIP File : D:\tmp\nourit\nourit.sh
File in ZIP File : D:\tmp\nourit\temp-nourit-file-name7880135018657413764.tmp
File in ZIP File : D:\tmp\nourit-test-1.txt
File in ZIP File : D:\tmp\nourit-test-2.txt
File in ZIP File : D:\tmp\nourit-test-3.txt
File in ZIP File : D:\tmp\nourit1.txt
File in ZIP File : D:\tmp\NOURITFile1.zip
File in ZIP File : D:\tmp\temp-nourit-file-name7880135018657413763.tmp
D:\tmp\NOURITFile.zip Created, Operation successful!

 

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*