Zip File Creation In Java Example

Chapter: ZIP Last Updated: 23-06-2016 16:43:33 UTC

Program:

            /* ............... START ............... */
                
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class JavaZipFileExample {
	public static void main(String[] args) {
		byte[] buffer = new byte[1024];

		try {

			FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
			ZipOutputStream zos = new ZipOutputStream(fos);
			ZipEntry ze = new ZipEntry("scan.log");
			zos.putNextEntry(ze);
			FileInputStream in = new FileInputStream("C:\\scan.log");

			int len;
			while ((len = in.read(buffer)) > 0) {
				zos.write(buffer, 0, len);
			}

			in.close();
			zos.closeEntry();

			// remember close it
			zos.close();

			System.out.println("Done");

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
                /* ............... END ............... */
        

Output

Creates MyFile.zip in C Drive.

Notes:

  • Java comes with java.util.zip library to perform data compression in ZIP format.
  • FileOutputStream to write to the file with the specified name, that is the zipFile.
  • ZipOutputStream from the FileOutputStream, that is an output stream filter for writing files in the ZIP file format.

Tags

Zip File, Java

Similar Programs Chapter Last Updated
Zip Entry Is Directory Example In Java ZIP 23-04-2016
Create Zip File With CRC32 Checksum In Java ZIP 23-04-2016
Create Zip File With Adler32 Checksum In Java ZIP 23-04-2016
Extract Zip File With Adler32 Checksum In Java ZIP 23-04-2016
Extract File With CRC32 Checksum In Java ZIP 23-04-2016
CRC32 Checksum In Java ZIP 10-08-2016
Adler32 Checksum In Java ZIP 23-04-2016
Read Zip File Checksum Value In Java Example ZIP 20-07-2016
Create Checksum For Zip File In Java ZIP 07-08-2017
Decompress Zip File Using ZipInputStream In Java ZIP 20-07-2016
Read Entries In Zip File In Java Example ZIP 20-07-2016
List The Contents Of Zip File In Java Example ZIP 07-08-2017
Extract Contents Of Zip File In Java Example ZIP 07-08-2017

1