Apache Commons Compress简介说明
下文笔者讲述Apache Commons Compress简介说明,如下所示
解压全部文件
可通过ZipArchiveOutputStream实现
如:
创建一个最大1MB的分卷zip文件
Apache Commons Compress简介说明
Apache Commons Compress是一个压缩、解压缩文件的组件 可以操作rar、cpio、Unix dump、tar、zip、gzip、XZ、Pack200和bzip2格式的压缩文件
解压
Compress对zip支持两种类型的解压 顺序解压和随机访问解压
ZipFile随机访问
使用名字解压特定文件 如/root/test.zip文件中 名称为targetFile的文件 解压为/tmp/output/targetFile文件 如下: ZipFile zipFile = new ZipFile("/root/test.zip"); ZipArchiveEntry entry = zipFile.getEntry("targetFile"); // 我们可以根据名字,直接找到要解压的文件 try (InputStream inputStream = zipFile.getInputStream(entry)) { // 这里inputStream就是一个正常的IO流,按照正常IO流的方式读取即可,这里简单给个例子 byte[] buffer = new byte[4096]; File outputFile = new File("/tmp/output/targetFile"); try (FileOutputStream fos = new FileOutputStream(outputFile)) { while (inputStream.read(buffer) > 0) { fos.write(buffer); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
解压全部文件
如要将/root/test.zip中的全部文件,解压到/tmp/output目录下
ZipFile zipFile = new ZipFile(new File("/root/test.zip")); byte[] buffer = new byte[4096]; ZipArchiveEntry entry; Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); // 获取全部文件的迭代器 InputStream inputStream; while (entries.hasMoreElements()) { entry = entries.nextElement(); if (entry.isDirectory()) { continue; } File outputFile = new File("/tmp/output/" + entry.getName()); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } inputStream = zipFile.getInputStream(entry); try (FileOutputStream fos = new FileOutputStream(outputFile)) { while (inputStream.read(buffer) > 0) { fos.write(buffer); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
ZipArchiveInputStream顺序访问
除了随机解压 Compress还支持顺序解压 对于一些IO场景 如网络IO之类 可把整个Zip文件读到内存中 然后再随机访问进行解压 不过如果遇到对一些比较大的zip 或内存敏感(如手机)这样的成本可能就太高
ZipArchiveInputStream:可一个文件一个文件的读取 在使用时可以决定解压或不解压遍历到的文件例:
File file = new File("/root/xxxxx/xx.zip"); // 这里还是用文件流举例,实际可以是各种InputStream try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new FileInputStream(file))) { ZipArchiveEntry entry = zipInputStream.getNextZipEntry(); entry.getName(); // 这里同样可以获取包括名字在内的许多文件信息 entry = zipInputStream.getNextZipEntry(); // 如果我们不需要读取第一个文件,可以直接跳到下一个文件 byte[] buffer = new byte[4096]; int read; while ((read = zipInputStream.read(buffer)) > 0) { XXXX } }
分卷文件解压
File lastSegmentFile = new File("/root/test.zip"); SeekableByteChannel channel = ZipsplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastSegmentFile); // 也可以通过指定所有zip分卷文件创建channel File firstSegmentFile = new File("/root/test.z01"); File secondSegmentFile = new File("/root/test.z02"); File thirdSegmentFile = new File("/root/test.zip"); SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forFiles(firstSegmentFile, secondSegmentFile, thirdSegmentFile);
压缩
Compress也支持创建zip压缩文件可通过ZipArchiveOutputStream实现
如:
File archive = new File("/root/xx.zip"); try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(archive)) { ZipArchiveEntry entry = new ZipArchiveEntry("testdata/test1.xml"); // 可以设置压缩等级 outputStream.setLevel(5); // 可以设置压缩算法,当前支持ZipEntry.DEFLATED和ZipEntry.STORED两种 outputStream.setMethod(ZipEntry.DEFLATED); // 也可以为每个文件设置压缩算法 entry.setMethod(ZipEntry.DEFLATED); // 在zip中创建一个文件 outputStream.putArchiveEntry(entry); // 并写入内容 outputStream.write("abcd\n".getBytes(Charset.forName("UTF-8"))); // 完成一个文件的写入 outputStream.closeArchiveEntry(); entry = new ZipArchiveEntry("testdata/test2.xml"); entry.setMethod(ZipEntry.STORED); outputStream.putArchiveEntry(entry); outputStream.write("efgh\n".getBytes(Charset.forName("UTF-8"))); outputStream.closeArchiveEntry(); }分卷创建
创建一个最大1MB的分卷zip文件
ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(archive, 1024 * 1024);通过上面的示例分析,我们可得出以下结论,如下所示
1.Compress的zip解压可以通过ZipFile和ZipArchiveInputStream实现 适用的场景: ZipFile: 适用于zip文件在硬盘里或内存里的情况,可以随机访问 ZipArchiveInputStream: 适用于通过网络IO或其他只能顺序读取zip的情况,只能顺序访问 2.压缩通过ZipArchiveOutputStream实现,可以传参以实现分卷压缩; 3.分卷解压通过ZipSplitReadOnlySeekableByteChannel实现;
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。