/**
* 文件打包工具类
*
* @author zlx
* @date 2021/09/09 17:09
**/
public class ZipUtils {
// InputStream 耗时20s左右
public static void zipFileNoBuffer(String zipFilePath, String... filePaths) {
File zipFile = new File(zipFilePath);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
for (String filePath : filePaths) {
try (InputStream input = new FileInputStream(filePath)) {
zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
int temp;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// BufferedInputStream 耗时2s左右
public static void zipFileBuffer(String zipFilePath, String... filePaths) {
File zipFile = new File(zipFilePath);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut)) {
for (String filePath : filePaths) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath))) {
zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
int temp;
while ((temp = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(temp);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// FileChannel 耗时1s左右
public static void zipFileChannel(String zipFilePath, String... filePaths) {
long beginTime = System.currentTimeMillis();
File zipFile = new File(zipFilePath);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
for (String filePath : filePaths) {
try (FileChannel fileChannel = new FileInputStream(filePath).getChannel()) {
zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
}
}
System.out.println(System.currentTimeMillis() - beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
//使用Map内存映射文件 耗时1s左右
public static void zipFileMap(String zipFilePath, String... filePaths) {
File zipFile = new File(zipFilePath);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
for (String filePath : filePaths) {
zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
//内存中的映射文件
MappedByteBuffer mappedByteBuffer = new RandomAccessFile(filePath, "r").getChannel()
.map(FileChannel.MapMode.READ_ONLY, 0, new File(filePath).length());
writableByteChannel.write(mappedByteBuffer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//使用Pipe 异步执行
public static void zipFilePip(String zipFilePath, String... filePaths) {
try (WritableByteChannel out = Channels.newChannel(new FileOutputStream(zipFilePath))) {
Pipe pipe = Pipe.open();
//异步任务
CompletableFuture.runAsync(() -> runTask(pipe, filePaths));
//获取读通道
ReadableByteChannel readableByteChannel = pipe.source();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (readableByteChannel.read(buffer) >= 0) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//异步任务
public static void runTask(Pipe pipe, String... filePaths) {
try (ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
WritableByteChannel out = Channels.newChannel(zos)) {
for (String filePath : filePaths) {
zos.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
File file = new File(filePath);
FileChannel jpgChannel = new FileInputStream(file).getChannel();
jpgChannel.transferTo(0, file.length(), out);
jpgChannel.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
10.文件打包工具类(jdk版)
最新推荐文章于 2023-05-26 17:43:40 发布