ToB企服应用市场:ToB评测及商务社交产业平台
标题:
JAVA 文件压缩(IO/NIO)
[打印本页]
作者:
道家人
时间:
2024-8-28 17:46
标题:
JAVA 文件压缩(IO/NIO)
IO
@GetMapping("io")
public void downloadZip() {
long start = System.currentTimeMillis();
EntityWrapper<AttachmentEntity> wrapper = new EntityWrapper<>();
List<AttachmentEntity> attachments = attachmentMapper.selectList(wrapper);
String zpiName = "D:/data/压缩文件io.zip";
try (FileOutputStream fos = new FileOutputStream(
zpiName); ZipOutputStream zipOutputStream = new ZipOutputStream(fos)) {
int i = 0;
for (AttachmentEntity attachment : attachments) {
String uuid = UUID.randomUUID().toString();
FileInputStream fileInputStream = new FileInputStream(attachment.getPath());
zipOutputStream.putNextEntry(new ZipEntry(uuid + "/" + attachment.getName()));
IOUtils.copy(fileInputStream, zipOutputStream);
zipOutputStream.closeEntry();
fileInputStream.close();
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));
}
复制代码
NIO
@GetMapping("nio")
public void downloadZipNio() throws FileNotFoundException {
long start = System.currentTimeMillis();
EntityWrapper<AttachmentEntity> wrapper = new EntityWrapper<>();
List<AttachmentEntity> attachments = attachmentMapper.selectList(wrapper);
String zpiName = "D:/data/压缩文件nio.zip";
try (FileOutputStream fos = new FileOutputStream(zpiName);
ZipOutputStream zos = new ZipOutputStream(fos);
WritableByteChannel wbc = Channels.newChannel(zos)) {
for (AttachmentEntity attachment : attachments) {
FileInputStream fis = new FileInputStream(attachment.getPath());
FileChannel channel = fis.getChannel();
String uuid = UUID.randomUUID().toString();
zos.putNextEntry(new ZipEntry(uuid + "/" + attachment.getName()));
channel.transferTo(0, channel.size(), wbc);
}
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));
}
复制代码
运行时间对比(ms)
ionio375349350321340330
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4