使用java ParallelScatterZipCreator快速压缩文件夹

时间:2016-12-30 08:52:58

标签: java multithreading parallel-processing zip scatter

InputStreamSupplier的值是什么或初始化的? 我试图压缩目录中的所有文件,这应该很快。 所以多线程是我想要的选择。

@import "global.less";
@import "fonts.less";
@import "variables.less";
@import "mixins.less";

FirstMain Class:

public class ScatterSample { 

    ParallelScatterZipCreator scatterZipCreator = new ParallelScatterZipCreator(); 
    ScatterZipOutputStream dirs = ScatterZipOutputStream.fileBased(File.createTempFile("scatter-dirs", "tmp")); 

    public ScatterSample() throws IOException { 
    } 

    public void addEntry(ZipArchiveEntry zipArchiveEntry, InputStreamSupplier streamSupplier) throws IOException { 
        if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink()) 
            dirs.addArchiveEntry(ZipArchiveEntryRequest.createZipArchiveEntryRequest(zipArchiveEntry, streamSupplier)); 
        else 
            scatterZipCreator.addArchiveEntry( zipArchiveEntry, streamSupplier); 
    } 

    public void writeTo(ZipArchiveOutputStream zipArchiveOutputStream) 
            throws IOException, ExecutionException, InterruptedException { 
        dirs.writeTo(zipArchiveOutputStream); 
        dirs.close(); 
        scatterZipCreator.writeTo(zipArchiveOutputStream); 
    } 

}

2 个答案:

答案 0 :(得分:0)

get()方法必须将InputStream返回到文件 您可以将内部类定义为:

static class FileInputStreamSupplier implements InputStreamSupplier {
    private Path sourceFile;

    FileInputStreamSupplier(Path sourceFile) {
        this.sourceFile = sourceFile;
    }

    @Override
    public InputStream get() {
        InputStream is = null;
        try {
            is = Files.newInputStream(sourceFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }
}

然后你可以调用:

scatterSample.addEntry(zipArchiveEntry, new FileInputStreamSupplier(file.toPath());

答案 1 :(得分:0)

您需要在ZipEntry中设置compress方法

ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(relativePath);
zipArchiveEntry.setMethod(ZipArchiveEntry.STORED);
scatterSample.addEntry(zipArchiveEntry, streamSupplier);

如果未设置compress方法,则该方法将引发异常。