在解压大文件时,利用线程池多线程解压文件提高解压效率,参考网络资源,实测有效:
private void decompress(String srcPath, String destPath) throws IOException {
long start = System.currentTimeMillis();
this.zipFile = new ZipFile(srcPath);
this.destPath = destPath;
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Log.i("ERIC", "// decompress()//==========entries.hasMoreElements()= " + entries.hasMoreElements());
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory()) {
continue;
}
threadPool.execute(new FileWritingTask(zipEntry));
}
Log.i("ERIC", "// decompress()//==========threadPool.shutdown()========== " );
threadPool.shutdown();
SystemProperties.set("persist.elmo.copyfiles", "false");
try {
threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
zipFile.close();
long end = System.currentTimeMillis();
// Log.i("ERIC","解压完成,所用时间为:" + (end - start) + "ms");
}
private class FileWritingTask implements Runnable {
private ZipEntry zipEntry;
FileWritingTask(ZipEntry zipEntry) {
this.zipEntry = zipEntry;
}
@Override
public void run() {
File file = new File(destPath + File.separator + zipEntry.getName());
Log.i("ERIC", "// FileWritingTask file= " + file.getAbsolutePath());
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
try (
InputStream inputStream = zipFile.getInputStream(this.zipEntry);
OutputStream outputStream = new FileOutputStream(file);
) {
int read;
byte[] bytes = new byte[inputStream.available()];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}