Java comes with “java.util.zip” library to perform data compression in ZIp format. The overall concept is quite straightforward.
2. Advance ZIP example – Recursively
Read all files from folder “C:\\testzip” and compress it into a zip file – “C:\\MyFile.zip“. It will recursively zip a directory as well.
Output
- Read file with “FileInputStream”
- Add the file name to “ZipEntry” and output it to “ZipOutputStream“
1. Simple ZIP example
Read a file “C:\\spy.log” and compress it into a zip file – “C:\\MyFile.zip“.package com.mkyong.zip; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class App { 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("spy.log"); zos.putNextEntry(ze); FileInputStream in = new FileInputStream("C:\\spy.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(); } } }
2. Advance ZIP example – Recursively
Read all files from folder “C:\\testzip” and compress it into a zip file – “C:\\MyFile.zip“. It will recursively zip a directory as well.
package com.mkyong.zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class AppZip { List<String> fileList; private static final String OUTPUT_ZIP_FILE = "C:\\MyFile.zip"; private static final String SOURCE_FOLDER = "C:\\testzip"; AppZip(){ fileList = new ArrayList<String>(); } public static void main( String[] args ) { AppZip appZip = new AppZip(); appZip.generateFileList(new File(SOURCE_FOLDER)); appZip.zipIt(OUTPUT_ZIP_FILE); } /** * Zip it * @param zipFile output ZIP file location */ public void zipIt(String zipFile){ byte[] buffer = new byte[1024]; try{ FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); System.out.println("Output to Zip : " + zipFile); for(String file : this.fileList){ System.out.println("File Added : " + file); ZipEntry ze= new ZipEntry(file); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file); 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(); } } /** * Traverse a directory and get all files, * and add the file into fileList * @param node file or directory */ public void generateFileList(File node){ //add file only if(node.isFile()){ fileList.add(generateZipEntry(node.getAbsoluteFile().toString())); } if(node.isDirectory()){ String[] subNote = node.list(); for(String filename : subNote){ generateFileList(new File(node, filename)); } } } /** * Format the file path for zip * @param file file path * @return Formatted file path */ private String generateZipEntry(String file){ return file.substring(SOURCE_FOLDER.length()+1, file.length()); } }
Output to Zip : C:\MyFile.zip File Added : pdf\Java-Interview.pdf File Added : spy\log\spy.log File Added : utf-encoded.txt File Added : utf.txt Done
jianbin1205
ReplyDeletecheap uggs
north face outlet online
coach outlet online
uggs outlet
snapback hats
true religion outlet,true religion jeans,true religion kids,true religion jeans sale,true religion jeans for men,true religion jacket,true religion sale
cheap oakley sunglasses
moncler coats
ugg boots on sale
cyber monday
kobe bryants shoes 2015
polo ralph lauren uk
moncler outlet
lululemon outlet
prada outlet
chanel handbags
ugg uk outlet
toms outlet
canada goose outlet
the north face jackets
new england patriots
chicago blackhawks
michael kors outlet sale
new york giants
air force one shoes
nike air max 90
ugg boots
air jordan 11 free shipping
michael kors handbags
nike trainers uk
air jordan shoes
kansas city chiefs
chicago bulls
Burberry Outlet
ReplyDeleteOakley Eyeglasses Michael Kors Outlet Coach Factory Outlet Coach Outlet Online Coach Purses Kate Spade Outlet Toms Shoes North Face Outlet Coach Outlet Gucci Belt North Face Jackets Oakley Sunglasses Toms Outlet North Face Outlet Nike Outlet Nike Hoodies Tory Burch Flats Marc Jacobs Handbags Jimmy Choo Shoes Jimmy Choos
Burberry Belt Tory Burch Boots Louis Vuitton Belt Ferragamo Belt Marc Jacobs Handbags Lululemon Outlet Christian Louboutin Shoes True Religion Outlet Tommy Hilfiger Outlet
Michael Kors Outlet Coach Outlet Red Bottoms Kevin Durant Shoes New Balance Outlet Adidas Outlet Coach Outlet Online Stephen Curry Jersey