優先java8 - どのように私は、Javaでマルチスレッドを持つ巨大なフォルダを解凍することができますか?

VitalyT:

:にReffering http://www.pixeldonor.com/2013/oct/12/concurrent-zip-compression-java-nio/

私はそれが30分についての私を取り、それは私が時間を短縮しようとしている、我々のアプリのためにたくさんある平均、5ギガバイトZIPファイルを解凍しようとしています。

私は、(デフォルトでは、私の書き込みチャンクは4096バイト)を組み合わせ、変更されたバッファサイズの多くを試してみましたNIO方法を変更し、ライブラリ、すべての結果はかなり同じですしました。

まだ試していないことの一つは、そのマルチスレッドチャンクでそれを読んで、チャンクでZIPファイルを分割することです。

スニペットコードは次のとおりです。

  private static ExecutorService e = Executors.newFixedThreadPool(20);
  public static void main(String argv[]) {
        try {
            String selectedZipFile = "/Users/xx/Documents/test123/large.zip";
            String selectedDirectory = "/Users/xx/Documents/test2";
            long st = System.currentTimeMillis();

            unzip(selectedDirectory, selectedZipFile);

            System.out.println(System.currentTimeMillis() - st);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


public static void unzip(String targetDir, String zipFilename) {
    ZipInputStream archive;
            try {
                List<ZipEntry> list = new ArrayList<>();
                archive = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilename)));
                ZipEntry entry;
                while ((entry = archive.getNextEntry()) != null) {
                    list.add(entry);
                }

                for (List<ZipEntry> partition : Lists.partition(list, 1000)) {
                    e.submit(new Multi(targetDir, partition, archive));
                }
            } catch (Exception e){
                e.printStackTrace();
            }
}

そして実行可能です:

  static class Multi implements Runnable {

    private List<ZipEntry> partition;
    private ZipInputStream zipInputStream;
    private String targetDir;

    public Multi(String targetDir, List<ZipEntry> partition, ZipInputStream zipInputStream) {
        this.partition = partition;
        this.zipInputStream = zipInputStream;
        this.targetDir = targetDir;
    }

    @Override
    public void run() {
        for (ZipEntry entry : partition) {
            File entryDestination = new File(targetDir, entry.getName());
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();

                BufferedOutputStream output = null;
                try {
                    int n;
                    byte buf[] = new byte[BUFSIZE];
                    output = new BufferedOutputStream(new FileOutputStream(entryDestination), BUFSIZE);
                    while ((n = zipInputStream.read(buf, 0, BUFSIZE)) != -1) {
                        output.write(buf, 0, n);
                    }
                    output.flush();


                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                } finally {

                    try {
                        output.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }
            }
        }
    }
}

しかし、その理由のために、それはファイルの内容のないディレクトリだけを格納し...

私の質問は:記事は、上記の「圧縮」の方法について大きなzipファイル上でマルチスレッドでチャンクを作るための正しい方法は何ですか?

アンドレアス:

Aは、ZipInputStreamデータの単一のストリームである、それは分割することはできません。

あなたはマルチスレッド解凍したい場合は、使用する必要がありますZipFileJavaの8ではあなたも無料でマルチスレッドを取得します。

public static void unzip(String targetDir, String zipFilename) {
    Path targetDirPath = Paths.get(targetDir);
    try (ZipFile zipFile = new ZipFile(zipFilename)) {
        zipFile.stream()
               .parallel() // enable multi-threading
               .forEach(e -> unzipEntry(zipFile, e, targetDirPath));
    } catch (IOException e) {
        throw new RuntimeException("Error opening zip file '" + zipFilename + "': " + e, e);
    }
}

private static void unzipEntry(ZipFile zipFile, ZipEntry entry, Path targetDir) {
    try {
        Path targetPath = targetDir.resolve(Paths.get(entry.getName()));
        if (Files.isDirectory(targetPath)) {
            Files.createDirectories(targetPath);
        } else {
            Files.createDirectories(targetPath.getParent());
            try (InputStream in = zipFile.getInputStream(entry)) {
                Files.copy(in, targetPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Error processing zip entry '" + entry.getName() + "': " + e, e);
    }
}

また、チェックアウトする場合があります。この答えは使用し、FileSystem真のJava 8経験のために、zipファイルのコンテンツにアクセスすることを。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=166711&siteId=1