Java解压多重zip压缩包
-- org.apache.commons.compress.archivers.zip.ZipFile结合ZipArchiveInputStream来实现
针对前一篇文章《Java打多重zip压缩包--
ByteArrayOutputStream结合org.apache.commons.compress来实现》打包后的zip文件如何解压的问题。
部分代码如下:
File f = new
File("c:/test.zip");
ZipFile zf = new ZipFile(f,
"gbk");
File folder = new
File("c:/test");
if (!folder.exists()) {
folder.mkdirs();
}
for
(Enumeration<ZipArchiveEntry> files = zf.getEntries();
files.hasMoreElements();) {
ZipArchiveEntry zae =
files.nextElement();
String zipname =
zae.getName();
if
(zipname.endsWith(".zip")) {
String
innerzip = StringUtils.removeEnd(zipname, ".zip");
File
innerfolder = new File(folder + File.separator + innerzip);
if
(!innerfolder.exists()) {
innerfolder.mkdirs();
}
ZipArchiveInputStream zais = new ZipArchiveInputStream(zf.getInputStream(zae),
"GBK", true);
FileOutputStream fos = null;
ZipArchiveEntry innerzae = null;
while
((innerzae = zais.getNextZipEntry()) != null) {
fos = new FileOutputStream(folder + File.separator + innerzip +
File.separator + innerzae.getName());
IOUtils.copy(zais, fos);
}
zais.close();
fos.flush();
fos.close();
} else {
ZipArchiveEntry packinfo = zf.getEntry(zipname);
String
filename = folder + File.separator + zipname;
FileOutputStream fos = new FileOutputStream(filename);
InputStream
is = zf.getInputStream(packinfo);
IOUtils.copy(is, fos);
is.close();
fos.flush();
fos.close();
}
}
zf.close();
|
解包后效果如下图:
参考资料:
1、《Java打多重zip压缩包 --
ByteArrayOutputStream结合org.apache.commons.compress来实现》
http://docs.google.com/View?id=ajfmzbdvh8wz_38ftxv59f6
2、Apache Commons Compress
http://commons.apache.org/compress/
3、本文地址
http://docs.google.com/View?id=ajfmzbdvh8wz_44cbjdbrfn
分享到:
相关推荐
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; // 创建一个带有密码的ZIP输出流 ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(outputStream); zipOut.setComment("这...
import org.apache.commons.compress.archivers.rar.RarArchiveOutputStream; public class ZipFileUtil { public void zipFiles(String sourceDir, String zipFilePath) throws Exception { // 使用ZipFile库...
同样,解压缩文件也很简单,以解压ZIP文件为例,我们可以使用`org.apache.commons.compress.archivers.zip.ZipFile`类: ```java import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org....
Java,包,NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile,压缩包中的commons-compress-1.20\commons-compress-1.20.jar应该能解决吧,不清楚
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; import java.util.Enumeration; import java.util.zip.ZipFile...
错误: java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile 缺少commons-compress-1.18.jar 或者maven项目引入下面依赖 <groupId>org.apache.commons <artifactId>commons-...
<groupId>org.apache.commons <artifactId>commons-compress 最新版本号 ``` 2. **压缩文件**:创建一个`ZipArchiveOutputStream`实例,然后使用`putArchiveEntry`方法添加每个文件到ZIP档案,最后调用`...
import org.apache.commons.compress.archivers.zip.ZipFile; public class CompressExample { public void extractImageFromZip(String zipFilePath, String entryName) { try (ZipFile zipFile = new ZipFile...
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.utils.IOUtils; import java.io.*; import java.nio.charset.Charset; import java.nio.file.Files; import java....
import org.apache.commons.compress.archivers.rar.RarArchiveInputStream; import org.apache.commons.compress.utils.IOUtils; public class RarExample { public static void main(String[] args) throws ...
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; ... ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(outputFile); zaos.putArchiveEntry(new ZipArchiveEntry(fileName))...
使用Apache Commons Compress,你可以通过`org.apache.commons.compress.archivers.zip.ZipArchiveEntry`和`ZipArchiveOutputStream`来创建ZIP文件,同时设置合适的字符编码,确保中文文件名能正确保存。 下面是一...
import org.apache.commons.compress.archivers.rar.RarArchiveInputStream; import java.io.*; public class RarExample { public static void main(String[] args) throws IOException { File rarFile = new ...
import org.apache.commons.compress.archivers.rar.RarArchiveInputStream; public class ExtractRar { public static void main(String[] args) throws IOException { File rarFile = new File("path_to_your_...
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.utils.IOUtils; // 假设我们已经有了File对象file代表jar包路径 File jarFile = ...; try ...
import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.utils.IOUtils; public class RecursiveUnzip { public static void main(String[] args) { String ...
import org.apache.commons.compress.archivers.zip.*; public class UnzipWithCommonsCompress { public static void unzipWithCommons(String zipFilePath, String destDirectory) throws IOException { File ...
import org.apache.commons.compress.archivers.zip.ZipFile; import java.io.File; import java.io.IOException; public class ZipUncompressor { public static void uncompress(String zipFilePath, String ...
然后,你可以使用`org.apache.commons.compress.archivers.rar.RarArchiveOutputStream`和`org.apache.commons.compress.archivers.rar.RarArchiveInputStream`来实现压缩和解压。但是,Apache Commons Compress对...
import org.apache.commons.compress.archivers.zip.ZipFile; ZipFile zipFile = new ZipFile("path_to_your_zip_file.zip"); for (ZipArchiveEntry entry : zipFile.getEntries()) { String entryName = entry....