使用java压缩文件成zip——三种方式压缩文件速度对比
生活随笔
收集整理的这篇文章主要介绍了
使用java压缩文件成zip——三种方式压缩文件速度对比
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
文章目录
- 写在前面
- 使用Buffered缓冲流压缩
- 使用nio的FileChannel压缩
- 使用apache.commons包下的并行压缩方式压缩
写在前面
对于大文件批量压缩的问题,实际上是非常消耗时间的,怎么能提高压缩速度呢?
在这里提供了三种方式压缩文件,咱们对比一下哪一个压缩速度更快。
在这里提供了三个文件,压缩后的大小大概是1.1GB左右,咱们测试一下每一种压缩方式的压缩时间。
使用Buffered缓冲流压缩
/*** zip文件压缩* @param inputFile 待压缩文件夹/文件名* @param outputFile 生成的压缩包名字*/public static void ZipCompress(String inputFile, String outputFile) throws Exception {//创建zip输出流ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));//创建缓冲输出流BufferedOutputStream bos = new BufferedOutputStream(out);File input = new File(inputFile);compress(out, bos, input,null);bos.close();out.close();}/*** @param name 压缩文件名,可以写为null保持默认*///递归压缩public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {if (name == null) {name = input.getName();}//如果路径为目录(文件夹)if (input.isDirectory()) {//取出文件夹中的文件(或子文件夹)File[] flist = input.listFiles();if (flist.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入{out.putNextEntry(new ZipEntry(name + "/"));} else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩{for (int i = 0; i < flist.length; i++) {compress(out, bos, flist[i], name + "/" + flist[i].getName());}}} else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中{out.putNextEntry(new ZipEntry(name));FileInputStream fos = new FileInputStream(input);BufferedInputStream bis = new BufferedInputStream(fos);int len;//将源文件写入到zip文件中byte[] buf = new byte[1024];while ((len = bis.read(buf)) != -1) {bos.write(buf,0,len);}bis.close();fos.close();}}public static void main(String[] args) {try {//压缩System.out.println("开始压缩");long date = new Date().getTime();ZipCompress("D:\\fileTest", "D:\\TestbyYTT.zip");System.out.println("压缩结束用时" + (new Date().getTime() - date));} catch (Exception e) {e.printStackTrace();}} 执行了三次,结果如下: 开始压缩 压缩结束用时46835开始压缩 压缩结束用时48016开始压缩 压缩结束用时47280结论:平均就是47秒左右。
使用nio的FileChannel压缩
/*** NIO方式压缩*/public static File fileToZip(String sourceFilePath, String zipFilePath, String fileName) throws IOException {File sourceFile = new File(sourceFilePath);FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;ZipOutputStream zos = null;WritableByteChannel writableByteChannel = null;File zipFile = new File(zipFilePath + "/" + fileName);if (!sourceFile.exists()) {System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");} else {try {if (zipFile.exists()) {Files.delete(zipFile.toPath());}File[] sourceFiles = sourceFile.listFiles();if (null == sourceFiles || sourceFiles.length < 1) {System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");} else {fos = new FileOutputStream(zipFile);zos = new ZipOutputStream(new BufferedOutputStream(fos));writableByteChannel = Channels.newChannel(zos) ;for (File file : sourceFiles) {//创建ZIP实体,并添加进压缩包ZipEntry zipEntry = new ZipEntry(file.getName());zos.putNextEntry(zipEntry);//读取待压缩的文件并写进压缩包里fis = new FileInputStream(file);FileChannel fileChannel = fis.getChannel();fileChannel.transferTo(0, file.length() -1,writableByteChannel);}}} finally {//关闭流if (null != bis) {bis.close();}if (fis != null) {fis.close();}if (zos != null) {zos.close();}if (fos != null) {fos.close();}if (null != writableByteChannel) {writableByteChannel.close();}}}return zipFile;} 执行了三次,结果如下: 开始压缩 压缩结束用时36929开始压缩 压缩结束用时37783开始压缩 压缩结束用时36246结论:平均36秒左右。
注意!transferTo方法最大能读取2G的文件!
使用apache.commons包下的并行压缩方式压缩
/*** @param sourceFilePath 需要压缩的目录* @param zipOutName 压缩后的文件名称**/public static void compressFileList(String sourceFilePath, String zipOutName) throws IOException, ExecutionException, InterruptedException {File sourceFile = new File(sourceFilePath);File[] sourceFiles = sourceFile.listFiles();ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("compressFileList-pool-").build();ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), factory);ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);OutputStream outputStream = new FileOutputStream(zipOutName);ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);zipArchiveOutputStream.setEncoding("UTF-8");for (File inFile : sourceFiles) {final InputStreamSupplier inputStreamSupplier = () -> {try {return new FileInputStream(inFile);} catch (FileNotFoundException e) {e.printStackTrace();return new NullInputStream(0);}};ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(inFile.getName());zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);zipArchiveEntry.setSize(inFile.length());zipArchiveEntry.setUnixMode(UnixStat.FILE_FLAG | 436);parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);}parallelScatterZipCreator.writeTo(zipArchiveOutputStream);zipArchiveOutputStream.close();outputStream.close();System.out.println("ParallelCompressUtil->ParallelCompressUtil-> info:{}"+ JSONObject.toJSONString(parallelScatterZipCreator.getStatisticsMessage()));} 执行了三次,结果如下: 开始压缩 ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":22642,"mergingElapsed":3802} 压缩结束用时27261开始压缩 ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":21958,"mergingElapsed":3406} 压缩结束用时25524开始压缩 ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":22076,"mergingElapsed":3668} 压缩结束用时25977结论:平均26秒左右。
总结
以上是生活随笔为你收集整理的使用java压缩文件成zip——三种方式压缩文件速度对比的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: debian下安装小企鹅输入法
- 下一篇: 《马克思主义基本原理概论》第 1 章世界