gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:47:54 28 4
gpt4 key购买 nike

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com