zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java实现多文件压缩和解压缩代码详解编程语言

JAVA文件编程语言代码 实现 详解 压缩 解压缩
2023-06-13 09:20:28 时间
public static void zipFiles(File[] srcfile, File zipfile) { byte[] buf = new byte[1024]; try { //ZipOutputStream类:完成文件或文件夹的压缩 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); for (int i = 0; i srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); out.putNextEntry(new ZipEntry(srcfile[i].getName())); int len; while ((len = in.read(buf)) 0) { out.write(buf, 0, len); out.closeEntry(); in.close(); out.close(); System.out.println("压缩完成."); } catch (Exception e) { e.printStackTrace(); /** * 功能:解压缩 * @param zipfile:需要解压缩的文件 * @param descDir:解压后的目标目录 public static void unZipFiles(File zipfile, String descDir) { try { ZipFile zf = new ZipFile(zipfile); for (Enumeration entries = zf.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zf.getInputStream(entry); OutputStream out = new FileOutputStream(descDir + zipEntryName); 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("解压缩完成."); } catch (Exception e) { e.printStackTrace(); /**功能: * @param args public static void main(String[] args) { //2个源文件 File f1 = new File("D://test//1.csv"); File f2 = new File("D://test//2.xlsx"); File[] srcfile = { f1, f2 }; //压缩后的文件 File zipfile = new File("D://test//3.zip"); TestZIP.zipFiles(srcfile, zipfile); //需要解压缩的文件 File file = new File("D://test//3.zip"); //解压后的目标目录 String dir = "D://test//1//"; TestZIP.unZipFiles(file, dir); }

转自:http://blog.csdn.net/zdp072/article/details/44177925 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10303.html

cjava