javaunzip⽅法
第⼀次⽅式是apache的zip包
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.16</version>
</dependency>
ZipFile zipFile = null;
int BUFFER_SIZE=1024;
try {
zipFile=new ZipFile(zip);
Enumeration<ZipArchiveEntry> entries = Entries();
ZipArchiveEntry entry = null;
while (entries.hasMoreElements()) {
entry = Element();
if (entry.isDirectory()) {
File directory = new File(zipPath, Name());
directory.mkdirs();
} else {
File file = new File(zipPath, Name());
if (!ParentFile().exists()) {
}
InputStream is = InputStream(entry);
OutputStream os = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(is);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
zipFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
第⼆种是⽤zip4j
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
public static void main(String[] args) {
String file1 = "d:\\worktemp\\hfhhByUnitPaper.zip";
String target = "D:\\worktemp";
mkdirs方法
String password = "password";
try {
ZipFile zipFile = new ZipFile(file1);
//zipFile.se
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
} catch (ZipException e) {
e.printStackTrace();
}
}
不建设⽤apache的ZipArchiveInputStream,会有很多问题..详细情况可以参考源码中的说明..