JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to zip a single file?


Description:

Below example shows how to zip a file using ZipOutputStream class.


Code:
package com.java2novice.zip;

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

public class MyFileZip {

	public void zipFile(String filePath){
		
        FileOutputStream fos = null;
        ZipOutputStream zipOut = null;
        FileInputStream fis = null;
		try {
			fos = new FileOutputStream("C:/testing.zip");
			zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
			File input = new File(filePath);
			fis = new FileInputStream(input);
			ZipEntry ze = new ZipEntry(input.getName());
			System.out.println("Zipping the file: "+input.getName());
			zipOut.putNextEntry(ze);
			byte[] tmp = new byte[4*1024];
			int size = 0;
			while((size = fis.read(tmp)) != -1){
				zipOut.write(tmp, 0, size);
			}
			zipOut.flush();
			zipOut.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try{
				if(fos != null) fos.close();
				if(fis != null) fis.close();
			} catch(Exception ex){
				
			}
		}
	}
	
	public static void main(String a[]){
		
		MyFileZip mfe = new MyFileZip();
		mfe.zipFile("C:/test.txt");
	}
}

Output:
Zipping the file: test.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
Prevent a method from being overridden
By specifying final keyword to the method you can avoid overriding in a subcalss. Similarlly one can use final at class level to prevent creating subclasses.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

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 © 2019 by Nataraja Gootooru. All Rights Reserved.