【问题标题】:Fast zipping folder using java ParallelScatterZipCreator使用 java ParallelScatterZipCreator 快速压缩文件夹
【发布时间】:2017-05-14 13:58:24
【问题描述】:

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

}

【问题讨论】:

    标签: java multithreading parallel-processing zip scatter


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      需要在ZipEntry中设置压缩方式

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

      如果不设置compress方法,方法会抛出异常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        • 2012-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        相关资源
        最近更新 更多