用于将整个存档解压缩到java中的目录的实用程序

我想在我的程序中做这样的事情:

File zipFile = .....; File destDir = ....; ImaginaryZipUtility.unzipAllTo(zipFile, destdir); 

我不可能是第一个从程序中做到这一点的人。 我在哪里找到像上面这样的实用方法? 我试着看看apache commons-io,但没有。 那么,我应该在哪里看?

非常古老的代码,我能够挖掘

 package com.den.frontend; import java.util.*; import java.util.zip.*; import java.io.*; public class ZIPUtility { public static final int BUFFER_SIZE = 2048; //This function converts the zip file into uncompressed files which are placed in the //destination directory //destination directory should be created first public static boolean unzipFiles(String srcDirectory, String srcFile, String destDirectory) { try { //first make sure that all the arguments are valid and not null if(srcDirectory == null) { System.out.println(1); return false; } if(srcFile == null) { System.out.println(2); return false; } if(destDirectory == null) { System.out.println(3); return false; } if(srcDirectory.equals("")) { System.out.println(4); return false; } if(srcFile.equals("")) { System.out.println(5); return false; } if(destDirectory.equals("")) { System.out.println(6); return false; } //now make sure that these directories exist File sourceDirectory = new File(srcDirectory); File sourceFile = new File(srcDirectory + File.separator + srcFile); File destinationDirectory = new File(destDirectory); // Prevent "Zip Slip" vulnerability https://snyk.io/research/zip-slip-vulnerability String canonicalDestinationFile = sourceFile.getCanonicalPath(); if (!canonicalDestinationFile.startsWith(destinationDirectory.getCanonicalPath() + File.separator)) { throw new Exception("Entry is outside of the target dir: " + e.getName()); } if(!sourceDirectory.exists()) { System.out.println(7); return false; } if(!sourceFile.exists()) { System.out.println(sourceFile); return false; } if(!destinationDirectory.exists()) { System.out.println(9); return false; } //now start with unzip process BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(sourceFile); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry = null; while((entry = zis.getNextEntry()) != null) { String outputFilename = destDirectory + File.separator + entry.getName(); System.out.println("Extracting file: " + entry.getName()); createDirIfNeeded(destDirectory, entry); int count; byte data[] = new byte[BUFFER_SIZE]; //write the file to the disk FileOutputStream fos = new FileOutputStream(outputFilename); dest = new BufferedOutputStream(fos, BUFFER_SIZE); while((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } //close the output streams dest.flush(); dest.close(); } //we are done with all the files //close the zip file zis.close(); } catch(Exception e) { e.printStackTrace(); return false; } return true; } private static void createDirIfNeeded(String destDirectory, ZipEntry entry) { String name = entry.getName(); if(name.contains("/")) { System.out.println("directory will need to be created"); int index = name.lastIndexOf("/"); String dirSequence = name.substring(0, index); File newDirs = new File(destDirectory + File.separator + dirSequence); //create the directory newDirs.mkdirs(); } } } 

我知道我已经很晚了,但是我一直在寻找同样的东西,并通过谷歌找到了这个。 我找到的最简单的方法是ant任务“解压缩”。 您可以使用它而不需要任何复杂的ant设置(有像ProjectHelper这样的东西来构建一个完整的ant项目)来自你的java源码,包括

  org.apache.ant ant-compress 1.2  

将文件解压缩到目录的代码如下所示

 org.apache.ant.compress.taskdefs.Unzip u = new Unzip(); u.setSrc(new File("")); u.setDest(new File("")); u.execute(); 

好吧, java.util.zip有一些类可以以非常直接的方式执行您想要的操作

看起来可以使用TrueZip库做我想做的事情: https ://truezip.dev.java.net/manual-6.html#Copying

这不是一个理想的情况,因为库非常大并且范围比我需要的大(并且还有一些特殊和令人困惑的细节,例如围绕java.io.File的子类组织,它们也被称为File用于通常也处理java.io.File实例的类!)。

至少我不必处于这样一种情况,即类中的大多数代码行与类的职责无关,或者在项目中维护一个与模块目的无关的复杂实用程序类。 。

我认为这是有经验的开发人员从Java迁移到Ruby的主要原因的典型示例。 尽管java中有大量的库,但是它们中的很多都设计得很差,因此简单的操作变得和更专业的操作一样难以执行。 似乎它们是自下而上由技术专家编写的,他们更渴望揭示所有细节和可能性,而不是简单地完成日常任务。 apache commons人们应该荣幸地创建库来减轻你的类与代码行,尤其是循环和条件,这与类的业务目的无关。