java - 使用 java ParallelScatterZipCreator 快速压缩文件夹

标签 java multithreading parallel-processing zip scatter

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

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); 
    } 

}

第一主类:

public class FirstMain {

    public FirstMain() {
        // TODO Auto-generated constructor stub
    }

    public static void compressFolder(String sourceFolder, String absoluteZipfilepath)
    {
        try
        {
            ScatterSample scatterSample=new ScatterSample();


            File srcFolder = new File(sourceFolder);
            if(srcFolder != null && srcFolder.isDirectory())
            {
                Iterator<File> i = FileUtils.iterateFiles(srcFolder, new String []{"pdf"}, true);


                File zipFile = new File(absoluteZipfilepath);

                OutputStream outputStream = new FileOutputStream(zipFile);

                ZipArchiveOutputStream zipArchiveOutputStream= new ZipArchiveOutputStream(outputStream);
                int srcFolderLength = srcFolder.getAbsolutePath().length() + 1;  // +1 to remove the last file separator

                while(i.hasNext())
                {
                    File file = i.next();                   
                    String relativePath  = file.getAbsolutePath().substring(srcFolderLength);



                    InputStreamSupplier streamSupplier=new InputStreamSupplier(){

                        @Override
                        public InputStream get() {
                            // TODO Auto-generated method stub
                            return null;
                        }
                    }; 

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


                }


                scatterSample.writeTo(zipArchiveOutputStream);

            }
        }catch (Exception e) {
            e.printStackTrace();
        }         
    }
     public static void main( String[] args )
     {
         compressFolder("C:\\Users\\akatm\\Desktop\\Stuff\\zipdata\\Newtry\\","C:/Users/akatm/Desktop/Stuff/Newtry.zip");
     }

}

最佳答案

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());

关于java - 使用 java ParallelScatterZipCreator 快速压缩文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393313/

相关文章:

java - 使用 execv 时 Cygwin C 文件返回 "Bad Address"

c++ - 为什么我在使用 qt 等待条件时不必包含锁定/解锁语句?

C# Monte Carlo Incremental Risk Calculation 优化,随机数,并行执行

c++ - 传递 "shared"指针的 OpenMP 任务

java - dao、tx、服务结构 : where to place a method querying an abstract entity?

java - Java 中按位运算的调试工具

使用比较器的 Java 集合二分搜索不起作用

linux - 如何告诉另一个线程一个线程在 recv() 调用中*现在*

c# - 如何在后台线程中创建 WPF 控件?

c++ - 使用与 openmp C++ 并行的循环计算矩阵中每一行的最小值