In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.