JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to zip a file using ZipFile class?


Description:

The ZipInputStream class reads ZIP files sequentially, and the class ZipFile, reads the contents of a ZIP file using a random access file internally so that the entries of the ZIP file do not have to be read sequentially. The below example shows how to zip files using ZipFile class.


Code:
package com.java2novice.zip;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class MyZipUsingZipFile {

	
	public void unzipFile(String filePath){
		
		ZipEntry zipEntry = null;
        ZipFile zipfile = null;
		try {
			zipfile = new ZipFile(filePath);
			Enumeration enmu = zipfile.entries();
			while(enmu.hasMoreElements()){
				FileOutputStream fos = null;
				InputStream is = null;
				try{
				zipEntry = (ZipEntry) enmu.nextElement();
				is = zipfile.getInputStream(zipEntry);
				byte[] tmp = new byte[4*1024];
				String opFilePath = "C:/"+zipEntry.getName();
				System.out.println("Extracting file to "+opFilePath);
				fos = new FileOutputStream(opFilePath);
				int size = 0;
				while((size = is.read(tmp)) != -1){
					fos.write(tmp, 0, size);
				}
				fos.flush();
				fos.close();
				is.close();
				} catch(Exception ex){
					
				} finally{
					try{
						if(fos != null) fos.close();
						if(is != null) is.close();
					} catch (Exception ex){}
				}
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	public static void main(String a[]){
		
		MyZipUsingZipFile mfe = new MyZipUsingZipFile();
		mfe.unzipFile("C:/Archive.zip");
	}
}

Output:
Extracting file to C:/config_copy.txt
Extracting file to C:/mac-addr_copy.txt
<< Previous Program | Next Program >>

List of all java.util.zip class sample examples:

  1. How to compress byte array in java?
  2. How to decompress byte array in java?
  3. How to zip a single file?
  4. How to zip multiple files?
  5. How to read zip files entries or file name list?
  6. How to unzip files in java?
  7. How to generate checksum value for for a file in java?
  8. How to compress and store objects using zip utility?
  9. How to decompress the compressed objects using zip utility?
  10. How to zip a file using ZipFile class?
  11. How to compress a file in GZip format?
  12. How to uncompress a file from GZip format?
Knowledge Centre
What is wrapper class?
Everything in java is an object, except primitives. Primitives are int, short, long, boolean, etc. Since they are not objects, they cannot return as objects, and collection of objects. To support this, java provides wrapper classes to move primitives to objects. Some of the wrapper classes are Integer, Long, Boolean, etc.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.