Java unzip zip - multiple files (including folders), as follows:
Compress multiple files and folders and decompress complex file directories.
The compression method uses variable parameters, which can compress 1 to multiple files. It can be written in an array or written into the parameter list one by one...
ZipFiles(zip,"abc",new File("d:/English"),new File("d:/issuing data.xls"));Test file directory structure:
The compressed content of the test: English folder and two excel files of the same level
File[] files = new File[]{new File("d:/English"),new File("d:/issuing data.xls"),new File("d:/Chinese name.xls")};Here is the compressed code:
/** * Compressed file - Since out needs to be outside the recursive call, encapsulate a method to * call ZipFiles(ZipOutputStream out,String path,File... srcFiles) * @param zip * @param path * @param srcFiles * @throws IOException * @author isea533 */ public static void ZipFiles(File zip,String path,File... srcFiles) throws IOException{ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip)); ZipTest.ZipFiles(out,path,srcFiles); out.close(); System.out.println("*********************************Compressed**********************"); } /** * Compressed file-File * @param zipFile zipfile* @param srcFiles The compressed source file* @author isea533 */ public static void ZipFiles(ZipOutputStream out,String path,File... srcFiles){ path = path.replaceAll("//*", "/"); if(!path.endsWith("/")){ path+="/"; } byte[] buf = new byte[1024]; try { for(int i=0;i<srcFiles.length;i++){ if(srcFiles[i].isDirectory()){ File[] files = srcFiles[i].listFiles(); String srcPath = srcFiles[i].getName(); srcPath = srcPath.replaceAll("//*", "/"); if(!srcPath.endsWith("/")){ srcPath+="/"; } out.putNextEntry(new ZipEntry(path+srcPath)); ZipFiles(out,path+srcPath,files); } else{ FileInputStream in = new FileInputStream(srcFiles[i]); System.out.println(path + srcFiles[i].getName()); out.putNextEntry(new ZipEntry(path + srcFiles[i].getName())); int len; while((len=in.read(buf))>0){ out.write(buf,0,len); } out.closeEntry(); in.close(); } } } catch (Exception e) { e.printStackTrace(); } }During compression, make judgments on the folder and then recursively compress the file.
Then unzip:
/** * Unzip to the specified directory* @param zipPath * @param descDir * @author isea533 */ public static void unZipFiles(String zipPath,String descDir)throws IOException{ unZipFiles(new File(zipPath), descDir); } /** * Unzip the file to the specified directory* @param zipFile * @param descDir * @author isea533 */ @SuppressWarnings("rawtypes") public static void unZipFiles(File zipFile,String descDir)throws IOException{ File pathFile = new File(descDir); if(!pathFile.exists()){ pathFile.mkdirs(); } ZipFile zip = new ZipFile(zipFile); for(Enumeration entries = zip.getEntries(); entries.hasMoreElements();){ ZipEntry entry = (ZipEntry)entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir+zipEntryName).replaceAll("//*", "/");; //Judge whether the path exists. If it does not exist, create the file path File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if(!file.exists()){ file.mkdirs(); } //Judge whether the full path of the file is a folder. If it has been uploaded above, there is no need to decompress if(new File(outPath).isDirectory()){ continue; } //Output file path information System.out.println(outPath); OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while((len=in.read(buf1))>0){ out.write(buf1,0,len); } in.close(); out.close(); } System.out.println("*********************************************"); }When decompressing, determine that the folder does not exist, and only create the folder, not decompress it... because decompression is for files, not folders, and folders need to be created by themselves.
Test method:
public static void main(String[] args) throws IOException { /** * Compressed file*/ File[] files = new File[]{new File("d:/English"),new File("d:/Export data.xls"),new File("d:/Chinese name.xls")}; File zip = new File("d:/Compressed.zip"); ZipFiles(zip,"abc",files); /** * Unzip file*/ File zipFile = new File("d:/Compressed.zip"); String path = "d:/zipfile/"; unZipFiles(zipFile, path); }The test method does not handle the exception, this is wrong, please do not imitate it.
Output result:
abc/English/templete.xls
abc/English/Chinese/csdn/isea/533/abc/templete.xls
abc/English/Chinese/csdn/isea/533/abc/zipfile2/templete.xls
abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/card collection list.xls
abc/English/Chinese/csdn/isea/533/abc/zipfile2/card collection list.xls
abc/English/Chinese/csdn/isea/templete.xls
abc/English/Chinese/csdn/isea/card collection list.xls
abc/English/Chinese/csdn/templete.xls
abc/English/Card Receive List.xls
abc/issuing data.xls
abc/Chinese name.xls
*********************Compressed completed***************************
d:/zipfile/abc/Chinese name.xls
d:/zipfile/abc/issue data.xls
d:/zipfile/abc/English/Card Receive List.xls
d:/zipfile/abc/English/Chinese/csdn/templete.xls
d:/zipfile/abc/English/Chinese/csdn/isea/card collection list.xls
d:/zipfile/abc/English/Chinese/csdn/isea/templete.xls
d:/zipfile/abc/English/Chinese/csdn/isea/533/abc/templete.xls
d:/zipfile/abc/English/templete.xls
d:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/templete.xls
d:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
d:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
d:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/card collection list.xls
d:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/Card-received list.xls
**********************************
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.