How to use java to compress multiple files into zip at one time
This article mainly shows you "how to use java to achieve one-time compression of multiple files to zip", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use java to compress multiple files to zip at a time" this article.
The details are as follows:
1. Packages need to be introduced:
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;import org.springframework.util.StringUtils
two。 Code
/ * * @ Title: compress* @ Description: TODO* @ param filePaths address list of files to be compressed (absolute path) * @ param zipFilePath to which zip file to compress (there is no need to create such a zip You only need to specify a full path) * @ param keepDirStructure whether the compressed directory maintains the original directory structure * @ throws IOException* @ return int the number of files successfully compressed * / public static int compress (List filePaths, String zipFilePath,Boolean keepDirStructure) throws IOException {byte [] buf = new byte [1024] File zipFile = new File (zipFilePath); / / if the zip file does not exist, a file is created to compress if (! zipFile.exists ()) zipFile.createNewFile (); how many files are compressed by int fileCount = 0? Try {ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (zipFile)); for (int I = 0; I
< filePaths.size(); i++){ String relativePath = filePaths.get(i); if(StringUtils.isEmpty(relativePath)){ continue; } File sourceFile = new File(relativePath);//绝对路径找到file if(sourceFile == null || !sourceFile.exists()){ continue; } FileInputStream fis = new FileInputStream(sourceFile); if(keepDirStructure!=null && keepDirStructure){ //保持目录结构 zos.putNextEntry(new ZipEntry(relativePath)); }else{ //直接放到压缩包的根目录 zos.putNextEntry(new ZipEntry(sourceFile.getName())); } //System.out.println("压缩当前文件:"+sourceFile.getName()); int len; while((len = fis.read(buf)) >0) {zos.write (buf, 0, len);} zos.closeEntry (); fis.close (); fileCount++;} zos.close (); / / System.out.println ("Compression complete");} catch (Exception e) {e.printStackTrace ();} return fileCount;}
3. test
Public static void main (String [] args) throws IOException {List sourceFilePaths = new ArrayList (); sourceFilePaths.add ("d:/test/C08065.jpg"); sourceFilePaths.add ("d:/test/ New folder / C08984.jpg"); sourceFilePaths.add ("d:/test/ cannot find me .jpg"); / / try a file that cannot be found / / specify which zip (absolute path) String zipTempFilePath = "D:/test/test.zip" to package to / / call to compress int s = compress (sourceFilePaths, zipTempFilePath,false); System.out.println ("successfully compress" + s + "files");} above is all the contents of the article "how to use java to compress multiple files into zip at one time". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!