如何使用支持 Zip64 的 ScatterZipOutputStream 实现并行 Zip 创建?

我想知道是否有人可以帮助使用 ScatterZipOutputStream 实现并行 Zip 创建。我进行了很多搜索,但没有在哪里找到相同的示例。


https://commons.apache.org/proper/commons-compress/zip.html


我尝试使用 ZipArchiveOutputStream 制作 Zip、压缩目录等。现在,我正在尝试并行执行此操作。


public static void makeZip(String filename) throws IOException,

        ArchiveException {

    File sourceFile = new File(filename);


    final OutputStream out = new FileOutputStream(filename.substring(0, filename.lastIndexOf('.')) + ".zip");

    ZipArchiveOutputStream os = new ZipArchiveOutputStream(out);

    os.setUseZip64(Zip64Mode.AsNeeded);


    os.putArchiveEntry(new ZipArchiveEntry(sourceFile.getName()));

    IOUtils.copy(new FileInputStream(sourceFile), os);

    os.closeArchiveEntry();

    os.close();

}

它应该能够将单个文件作为线程处理,然后将其组合以编写结果 zip。


慕雪6442864
浏览 200回答 1
1回答

慕的地6264312

以下是zipand的工作代码unzip:1. 更改sourceFolderand的路径zipFilePath2. 仅压缩 *.text 类型的文件,它可以是任何类型或所有文件3. 解压缩文件位于sourceFolder/unzip/在 build.gradle 或 pom.xml 中导入以下依赖项implementation("org.apache.commons:commons-compress:1.18")implementation("commons-io:commons-io:2.6")参考:https ://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.18 https://mvnrepository.com/artifact/commons-io/commons-io/2.6//代码import org.apache.commons.compress.archivers.zip.*;import org.apache.commons.compress.parallel.InputStreamSupplier;import org.apache.commons.io.FileUtils;import java.io.*;import java.nio.file.Files;import java.util.Iterator;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class ZipMain {static ParallelScatterZipCreator scatterZipCreator = new ParallelScatterZipCreator();static ScatterZipOutputStream dirs;static {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; dirs = ScatterZipOutputStream.fileBased(File.createTempFile("java-zip-dirs", "tmp"));&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}public static void main(String[] args) throws IOException {&nbsp; &nbsp; String sourceFolder = "/Users/<user>/Desktop/";&nbsp; &nbsp; String zipFilePath = "/Users/<user>/Desktop/Desk.zip";&nbsp; &nbsp; String fileTypesToBeAddedToZip = "txt";&nbsp; &nbsp; zip(sourceFolder, zipFilePath, fileTypesToBeAddedToZip);&nbsp; &nbsp; unzip(zipFilePath, sourceFolder + "/unzip/");}private static void zip(String sourceFolder, String zipFilePath, String fileTypesToBeAddedToZip) throws IOException {&nbsp; &nbsp; OutputStream outputStream = null;&nbsp; &nbsp; ZipArchiveOutputStream zipArchiveOutputStream = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; File srcFolder = new File(sourceFolder);&nbsp; &nbsp; &nbsp; &nbsp; if (srcFolder.isDirectory()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // uncomment following code if you want to add all files under srcFolder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Iterator<File> fileIterator = Arrays.asList(srcFolder.listFiles()).iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<File> fileIterator = FileUtils.iterateFiles(srcFolder, new String[]{fileTypesToBeAddedToZip}, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File zipFile = new File(zipFilePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipFile.delete();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputStream = new FileOutputStream(zipFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int srcFolderLength = srcFolder.getAbsolutePath().length() + 1;&nbsp; // +1 to remove the last file separator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (fileIterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = fileIterator.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // uncomment following code if you want to add all files under srcFolder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (file.isDirectory()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String relativePath = file.getAbsolutePath().substring(srcFolderLength);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStreamSupplier streamSupplier = () -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream is = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is = Files.newInputStream(file.toPath());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return is;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(relativePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipArchiveEntry.setMethod(ZipEntry.DEFLATED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scatterZipCreator.addArchiveEntry(zipArchiveEntry, streamSupplier);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scatterZipCreator.writeTo(zipArchiveOutputStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (zipArchiveOutputStream != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipArchiveOutputStream.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; if (outputStream != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputStream.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private static void unzip(String zipFilePath, String destDir) {&nbsp; &nbsp; File dir = new File(destDir);&nbsp; &nbsp; // create output directory if it doesn't exist&nbsp; &nbsp; if (!dir.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; dir.mkdirs();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; dir.delete();&nbsp; &nbsp; }&nbsp; &nbsp; FileInputStream fis;&nbsp; &nbsp; //buffer for read and write data to file&nbsp; &nbsp; byte[] buffer = new byte[1024];&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; fis = new FileInputStream(zipFilePath);&nbsp; &nbsp; &nbsp; &nbsp; ZipInputStream zis = new ZipInputStream(fis);&nbsp; &nbsp; &nbsp; &nbsp; ZipEntry ze = zis.getNextEntry();&nbsp; &nbsp; &nbsp; &nbsp; while (ze != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileName = ze.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File newFile = new File(destDir + File.separator + fileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Unzipping to " + newFile.getAbsolutePath());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //create directories for sub directories in zip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String parentFolder = newFile.getParent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File folder = new File(parentFolder);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; folder.mkdirs();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream fos = new FileOutputStream(newFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int len;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((len = zis.read(buffer)) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fos.write(buffer, 0, len);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fos.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //close this ZipEntry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zis.closeEntry();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ze = zis.getNextEntry();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //close last ZipEntry&nbsp; &nbsp; &nbsp; &nbsp; zis.closeEntry();&nbsp; &nbsp; &nbsp; &nbsp; zis.close();&nbsp; &nbsp; &nbsp; &nbsp; fis.close();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java