java实现zip,gzip,7z,zlib格式的压缩打包
本文主要介绍的是通过使用java的相关类可以实现对文件或文件夹的压缩。
zlib是一种数据压缩程序库,它的设计目标是处理单纯的数据(而不管数据的来源是什么)。
7z 是一种新的压缩格式,它拥有目前最高的压缩比。
gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格式),它的设计目标是处理单个的文件。gzip在压缩文件中的数据时使用的就是zlib。为了保存与文件属性有关的信息,gzip需要在压缩文件(*.gz)中保存更多的头信息内容,而zlib不用考虑这一点。但gzip只适用于单个文件,所以我们在UNIX/Linux上经常看到的压缩包后缀都是*.tar.gz或*.tgz,也就是先用tar把多个文件打包成单个文件,再用gzip压缩的结果。
zip是适用于压缩多个文件的格式(相应的工具有PkZip和WinZip等),因此,zip文件还要进一步包含文件目录结构的信息,比gzip的头信息更多。但需要注意,zip格式可采用多种压缩算法,我们常见的zip文件大多不是用zlib的算法压缩的,其压缩数据的格式与gzip大不一样。
所以,你应当根据你的具体需求,选择不同的压缩技术:如果只需要压缩/解压缩数据,你可以直接用zlib实现,如果需要生成gzip格式的文件或解压其他工具的压缩结果,你就必须用gzip或zip等相关的类来处理了。
maven依赖
1 2 3 4 5 | <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version> 1.12 </version> </dependency> |
zip格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static void zip(String input, String output, String name) throws Exception { ZipOutputStream out = new ZipOutputStream( new FileOutputStream(output)); String[] paths = input.split( "\\|" ); File[] files = new File[paths.length]; byte [] buffer = new byte [ 1024 ]; for ( int i = 0 ; i < paths.length; i++) { files[i] = new File(paths[i]); } for ( int i = 0 ; i < files.length; i++) { FileInputStream fis = new FileInputStream(files[i]); if (files.length == 1 && name != null ) { out.putNextEntry( new ZipEntry(name)); } else { out.putNextEntry( new ZipEntry(files[i].getName())); } int len; // 读入需要下载的文件的内容,打包到zip文件 while ((len = fis.read(buffer)) > 0 ) { out.write(buffer, 0 , len); } out.closeEntry(); fis.close(); } out.close(); } |
gzip打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public static void gzip(String input, String output, String name) throws Exception { String compress_name = null ; if (name != null ) { compress_name = name; } else { compress_name = new File(input).getName(); } byte [] buffer = new byte [ 1024 ]; try { GzipParameters gp = new GzipParameters(); //设置压缩文件里的文件名 gp.setFilename(compress_name); GzipCompressorOutputStream gcos = new GzipCompressorOutputStream( new FileOutputStream(output), gp); FileInputStream fis = new FileInputStream(input); int length; while ((length = fis.read(buffer)) > 0 ) { gcos.write(buffer, 0 , length); } fis.close(); gcos.finish(); } catch (IOException ioe) { ioe.printStackTrace(); } } |
7z打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public static void z7z(String input, String output, String name) throws Exception { try { SevenZOutputFile sevenZOutput = new SevenZOutputFile( new File(output)); SevenZArchiveEntry entry = null ; String[] paths = input.split( "\\|" ); File[] files = new File[paths.length]; for ( int i = 0 ; i < paths.length; i++) { files[i] = new File(paths[i].trim()); } for ( int i = 0 ; i < files.length; i++) { BufferedInputStream instream = null ; instream = new BufferedInputStream( new FileInputStream(paths[i])); if (name != null ) { entry = sevenZOutput.createArchiveEntry( new File(paths[i]), name); } else { entry = sevenZOutput.createArchiveEntry( new File(paths[i]), new File(paths[i]).getName()); } sevenZOutput.putArchiveEntry(entry); byte [] buffer = new byte [ 1024 ]; int len; while ((len = instream.read(buffer)) > 0 ) { sevenZOutput.write(buffer, 0 , len); } instream.close(); sevenZOutput.closeArchiveEntry(); } sevenZOutput.close(); } catch (IOException ioe) { System.out.println(ioe.toString() + " " + input); } } |
zlib打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void zlib(String input, String output) throws Exception { // DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(output)); DeflateParameters dp = new DeflateParameters(); dp.setWithZlibHeader( true ); DeflateCompressorOutputStream dcos = new DeflateCompressorOutputStream( new FileOutputStream(output),dp); FileInputStream fis = new FileInputStream(input); int length = ( int ) new File(input).length(); byte data[] = new byte [length]; // int length; while ((length = fis.read(data)) > 0 ) { dcos.write(data, 0 , length); } fis.close(); dcos.finish(); dcos.close(); } |
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2013-11-23 DB2与Sybase/Oracle/Informix的比较
2013-11-23 Spring3.1.2与Hibernate4.1.8整合
2013-11-23 c3p0、dbcp、proxool、BoneCP比较