java - 如何让解压任务更快?
问题描述
我想解压缩文件,我的代码正在运行,但速度很慢。示例:如果我想解压缩一个大小为 4 MB 的文件,大约需要 40 分钟才能完成。有没有办法让任务更快?
我的代码:
public class DecompressManager extends DownloadManager {
public static final int DECOMPRESS_SUCCESS = 101;
public static final int DECOMPRESS_FAILED = 102;
public DecompressManager(DownloadListener downloadListener) {
super(downloadListener);
}
public int DecompressTask(File zipFile, File targetDirectory){
updateTaskProgress("Starting unzip file",-1,null);
int result = DECOMPRESS_FAILED;
//unzip file
ZipInputStream zis = null;
try {
zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
}
if (ze.isDirectory())
continue;
try (FileOutputStream fout = new FileOutputStream(file)) {
long total = 0;
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
total += count;
updateTaskProgress("Decompress task",(int) ((total * 100) / ze.getSize()),null);
}
}
}
result = DECOMPRESS_SUCCESS;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert zis != null;
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
解决方案
推荐阅读
- r - 使用 map 函数从列表中进行子集试验
- arrays - 从右到左的关联性与指针
- jruby - OpenJFX TreeTableView 渲染问题
- jenkins - 由于不满足的依赖关系,某些插件无法加载。修复这些问题并重启 Jenkins
- javascript - ajax post var event.data
- python - 定义单链表的函数
- r - 如何合并图并在新图中保存有向和无向分量?
- python-3.x - 在 matplotlib 上添加标签和垂直线(绘图区域外)
- pandas - 在新的 MacBook Air M1 上安装 Pandas 时遇到问题
- c - 如何访问另一个结构内的链表的元素?