常用压缩方式
测试结果仅为小文本测试,压缩时间以及占用内存差距不大
普通压缩
public static void toZip1(List<File> srcFiles, OutputStream out) {
long start = System.currentTimeMillis();
try (ZipOutputStream zos = new ZipOutputStream(out);) {
for (File srcFile : srcFiles) {
zos.putNextEntry(new ZipEntry(srcFile.getName()));
FileInputStream in = new FileInputStream(srcFile);
IOUtils.copy(in, zos);
in.close();
zos.closeEntry();
}
long end = System.currentTimeMillis();
log.info("1压缩完成,耗时:{} ms", (end - start));
} catch (Exception e) {
e.printStackTrace();
}
}
内存映射
public static void toZip2(List<File> srcFiles, OutputStream out) {
long start = System.currentTimeMillis();
try (ZipOutputStream zipOut = new ZipOutputStream(out);
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
for (File srcFile : srcFiles) {
zipOut.putNextEntry(new ZipEntry(srcFile.getName()));
FileInputStream fileInputStream = new FileInputStream(srcFile);
FileChannel channel = fileInputStream.getChannel();
//内存中的映射文件
//FileChannel r = new RandomAccessFile(path, "r").getChannel();
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
writableByteChannel.write(mappedByteBuffer);
}
long end = System.currentTimeMillis();
log.info("2压缩完成,耗时:{} ms", (end - start));
} catch (Exception e) {
e.printStackTrace();
}
}
管道(NIO Pipe)
public static void toZip3(List<File> srcFiles, OutputStream out) {
long start = System.currentTimeMillis();
try (WritableByteChannel writeChannel = Channels.newChannel(out)) {
Pipe pipe = Pipe.open();
//异步任务
CompletableFuture.runAsync(() -> runTask(pipe, srcFiles));
//获取读通道
ReadableByteChannel readableByteChannel = pipe.source();
ByteBuffer buffer = ByteBuffer.allocate((1024));
while (readableByteChannel.read(buffer) >= 0) {
buffer.flip();
writeChannel.write(buffer);
buffer.clear();
}
long end = System.currentTimeMillis();
log.info("3压缩完成,耗时:{} ms", (end - start));
} catch (Exception e) {
e.printStackTrace();
}
}
//异步任务
public static void runTask(Pipe pipe, List<File> srcFiles) {
try (ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
WritableByteChannel writeChannel = Channels.newChannel(zos)) {
for (File srcFile : srcFiles) {
zos.putNextEntry(new ZipEntry(srcFile.getName()));
FileChannel fileChannel = new FileInputStream(srcFile).getChannel();
fileChannel.transferTo(0, fileChannel.size(), writeChannel);
fileChannel.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
通道(NIO Channel)
public static void toZip14(List<File> srcFiles, OutputStream out) {
//开始时间
long start = System.currentTimeMillis();
try (ZipOutputStream zipOut = new ZipOutputStream(out);
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
for (File srcFile : srcFiles) {
try (FileChannel fileChannel = new FileInputStream(srcFile).getChannel()) {
zipOut.putNextEntry(new ZipEntry(srcFile.getName()));
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
}
}
long end = System.currentTimeMillis();
log.info("4压缩完成,耗时:{} ms", (end - start));
} catch (Exception e) {
e.printStackTrace();
}
}
测试示例
public static void main(String[] args) throws FileNotFoundException {
List<File> srcFiles = new ArrayList<>();
srcFiles.add(new File("文本1.txt"));
srcFiles.add(new File("文本2.txt"));
srcFiles.add(new File("文本3.txt"));
srcFiles.add(new File("文本4.txt"));
FileOutputStream fileOutputStream1 = new FileOutputStream("zip1.zip");
FileOutputStream fileOutputStream2 = new FileOutputStream("zip2.zip");
FileOutputStream fileOutputStream3 = new FileOutputStream("zip3.zip");
FileOutputStream fileOutputStream4 = new FileOutputStream("zip4.zip");
toZip1(srcFiles, fileOutputStream1);
toZip1(srcFiles, fileOutputStream2);
toZip1(srcFiles, fileOutputStream3);
toZip1(srcFiles, fileOutputStream4);
}