gpt4 book ai didi

Java:解压缩比压缩慢得多

转载 作者:行者123 更新时间:2023-11-30 09:48:09 27 4
gpt4 key购买 nike

我有一些压缩文件的代码,通过网络发送它,然后在另一端解压缩它。我仍在测试代码,源和目标是相同的。压缩文件大约需要一分钟。解压缩文件大约需要一个小时。我认为我的代码中一定存在缺陷才能产生如此大的差异。这是解压缩的代码:

public String uncompressLocalZip(String filename,String strUUID,ParentEntry pe,boolean bControlFileProgress) {
final int BUFFER = 2048;
BufferedOutputStream out = null;
ZipInputStream zis = null;

try {

FileInputStream fis = new FileInputStream(Constants.conf.getFileDirectory() + Constants.PATH_SEPARATOR + strUUID + Constants.PATH_SEPARATOR + filename);
zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
long totallength = 0;
long size = 0;
if (pe !=null)
size = pe.getSize();


while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk

File fileOutput = new File(Constants.conf.getFileDirectory() + Constants.PATH_SEPARATOR + strUUID + Constants.PATH_SEPARATOR + Constants.conf.getUncompressFolderName() + Constants.PATH_SEPARATOR + entry.getName());
new File(fileOutput.getParent()).mkdirs();


BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fileOutput));

out = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
totallength += count;

}
out.flush();

}

}
catch(Exception e) {
e.printStackTrace();
return("FAILED");
}
finally {
try {if ( out!= null) out.close();} catch (IOException ioe) {}
try {if ( zis!= null) zis.close();} catch (IOException ioe) {}

}

return("SUCCESS");



}

这里是压缩代码:

public void createLocalZip(String filename,ProcessEntry pe) {
ZipOutputStream out=null;
try {

File fileOutput = new File (filename);
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fileOutput)));
long totallength=0;
long size = pe.getParentEntry().getSize();

String strStartDirectory;
if (pe.getParentEntry().isDirectory())
strStartDirectory=pe.getParentEntry().getUrl();
else
strStartDirectory=pe.getParentEntry().getFolder();



for (int i=0;i<pe.getParentEntry().tableModel3.getRowCount();i++) {
FileEntry fe = pe.getParentEntry().tableModel3.getFileEntry(i);
File fileInput = new File (fe.getUrl());
FileInputStream input = new FileInputStream(fileInput);
BufferedInputStream in = new BufferedInputStream(input);

String strRelativeDir = fe.getUrl().substring(strStartDirectory.length()+1,fe.getUrl().length());

ZipEntry entry = new ZipEntry(strRelativeDir);

out.putNextEntry(entry);


byte[] bbuf = new byte[2048];
int length=0;




while ((in != null) && ((length = in.read(bbuf)) != -1)) {

out.write(bbuf,0,length);
totallength += length;
pe.setProgress((int) (totallength*100/size));

}

in.close();


}






}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
try {if (out!=null) out.close();} catch(IOException ioe){}
}


}

更新:此特定测试的压缩率约为 90%(1.2GB 降至约 100MB)。所以我想这可能是解压缩与压缩的额外磁盘写入,尽管我预计差异接近 10 倍与 60 倍。

最佳答案

不要用 BufferedOutputStream 双重包装您的 OutputStream(您只需要 1 个 BufferedOutputStream 包装器),并在完成写入后将其关闭。

此外,ZipEntry 可以是目录,因此请检查并进行相应处理。

关于Java:解压缩比压缩慢得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6380366/

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