Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code for Java to compress a file into a zip file?

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Java implementation of the file compression into zip file code how to write," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn how to write Java code to compress files into zip files.

implementation code

ackage org.fh.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/** * Description: java compressed into zip * Author: FH Admin * from: fhadmin.cn */public class FileZip { /** * @param inputFileName Folder you want to compress (full path) * @param zipFileCompressed file (full path) * @throws Exception */ public static Boolean zip(String inputFileName, String zipFileName) throws Exception { zip(zipFileName, new File(inputFileName)); return true; } private static void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); zip(out, inputFile, ""); out.flush(); out.close(); } private static void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { File[] fl = f.listFiles(); out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i

< fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } } public static void main(String [] temp){ try { zip("E:\\ftl","E:\\test.zip");//你要压缩的文件夹 和 压缩后的文件 }catch (Exception ex) { ex.printStackTrace(); } }} 代码解释: 1.模型管理 :web在线流程设计器、导入导出xml、复制流程、部署流程 2.流程管理 :导入导出流程资源文件、查看流程图、根据流程实例反射出流程模型、激活挂起 3.运行中流程:查看流程信息、当前任务节点、当前流程图、作废暂停流程、指派待办人、自由跳转 4.历史的流程:查看流程信息、流程用时、流程状态、查看任务发起人信息 5.待办任务 :查看本人个人任务以及本角色下的任务、办理、驳回、作废、指派一下代理人 6.已办任务 :查看自己办理过的任务以及流程信息、流程图、流程状态(作废 驳回 正常完成) 补充 当然Java不仅能实现将文件压缩成zip文件,还可以实现将zip文件解压 下面是实现的工具类的核心代码,可以参考一下 /** * 解压到指定目录 * @param zipPath * @param descDir * @author isea533 */ public static void unZipFiles(String zipPath,String descDir)throws IOException{ unZipFiles(new File(zipPath), descDir); } /** * 解压文件到指定目录 * @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("\\*", "/");; //判断路径是否存在,不存在则创建文件路径 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if(!file.exists()){ file.mkdirs(); } //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 if(new File(outPath).isDirectory()){ continue; } //输出文件路径信息 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("******************** Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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: 295

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report