设为首页 收藏本站
查看: 913|回复: 0

[经验分享] java压缩与解压缩文件(利用apache的ant.jar) .

[复制链接]

尚未签到

发表于 2015-11-13 15:36:36 | 显示全部楼层 |阅读模式
  zip扮演着归档和压缩两个角色;gzip并不将文件归档,仅只是对单个文件进行压缩,所以,在UNIX平台上,命令tar通常用来创建一个档案文件,然后命令gzip来将档案文件压缩。

Java I/O类库还收录了一些能读写压缩格式流的类。要想提供压缩功能,只要把它们包在已有的I/O类的外面就行了。这些类不是Reader和Writer,而是InputStream和OutStreamput的子类。这是因为压缩算法是针对byte而不是字符的。

相关类与接口:
Checksum接口:被类Adler32和CRC32实现的接口
Adler32:使用Alder32算法来计算Checksum数目
CRC32:使用CRC32算法来计算Checksum数目

  
CheckedInputStream:InputStream派生类,可得到输入流的校验和Checksum,用于校验数据的完整性
CheckedOutputStream:OutputStream派生类,可得到输出流的校验和Checksum,
用于校验数据的完整性
  
DeflaterOutputStream:压缩类的基类。
ZipOutputStream:DeflaterOutputStream的一个子类,把数据压缩成Zip文件格式。
GZIPOutputStream:DeflaterOutputStream的一个子类,把数据压缩成GZip文件格式

  
InflaterInputStream:解压缩类的基类
ZipInputStream:InflaterInputStream的一个子类,能解压缩Zip格式的数据
GZIPInputStream:InflaterInputStream的一个子类,能解压缩Zip格式的数据

  
ZipEntry类:表示 ZIP 文件条目
ZipFile类:此类用于从 ZIP 文件读取条目


用GZIP进行对单个文件压缩
  GZIP的接口比较简单,因此如果你只需对一个流进行压缩的话,可以使用它。当然它可以压缩字符流,与可以压缩字节流,下面是一个对GBK编码格式的文本文件进行压缩的。
压缩类的用法非常简单;只要用GZIPOutputStream 或ZipOutputStream把输出流包起来,再用GZIPInputStream 或ZipInputStream把输入流包起来就行了。剩下的都是些普通的I/O操作。

  
[java] view plaincopyprint?

  • package com.apache.gzip;  
  •   
  • import java.io.BufferedInputStream;  
  • import java.io.BufferedOutputStream;  
  • import java.io.File;  
  • import java.io.FileInputStream;  
  • import java.io.FileNotFoundException;  
  • import java.io.FileOutputStream;  
  • import java.io.IOException;  
  • import java.util.Enumeration;  
  • import java.util.zip.CRC32;  
  • import java.util.zip.CheckedInputStream;  
  • import java.util.zip.CheckedOutputStream;  
  • import java.util.zip.Deflater;  
  • import java.util.zip.ZipException;  
  • import java.util.zip.ZipInputStream;  
  • import org.apache.tools.zip.ZipEntry;  
  • import org.apache.tools.zip.ZipFile;  
  • import org.apache.tools.zip.ZipOutputStream;  
  •   
  • /** 利用apache提供的ant.jar,提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径
  • * @Title:
  • * @Description:ZipCompress
  • * @Version 1.2
  • */  
  • public class ZipCompress {  
  •   
  •     private static boolean isCreateSrcDir = true;//是否创建源目录  
  •   
  •     /**
  •      * @param args
  •      * @throws IOException
  •      */  
  •     public static void main(String[] args) throws IOException {  
  •         String src = "f:\\中文包";//指定压缩源,可以是目录或文件  
  •         String decompressDir = "f:\\depress";//解压路径  
  •         String archive = "f:\\中文压缩文件.zip";//压缩包路径  
  •         String comment = "Java Zip 测试.";//压缩包注释  
  •         //----压缩文件或目录   
  •         writeByApacheZipOutputStream(src,archive,comment);  
  •         /*
  •          * 读压缩文件,注释掉,因为使用的是apache的压缩类,所以使用java类库中
  •          * 解压类时出错,这里不能运行
  •          */        
  •         readByZipInputStream(archive, decompressDir);  
  •         //----使用apace ZipFile读取压缩文件   
  •         readByApacheZipFile(archive, decompressDir);  
  •     }  
  •     /**对文件夹或者文件进行压缩
  •      *  
  •      * @Time 2012-3-9 上午09:32:35 create
  •      * @param src
  •      * @param archive
  •      * @param comment
  •      * @throws FileNotFoundException
  •      * @throws IOException
  •      * @author jiangzhenming
  •      */  
  •     public static void writeByApacheZipOutputStream(String src, String archive,  
  •             String comment) throws FileNotFoundException, IOException {  
  •         //----压缩文件:   
  •         FileOutputStream f = new FileOutputStream(archive);  
  •         //使用指定校验和创建输出流   
  •         CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());  
  •   
  •         ZipOutputStream zos = new ZipOutputStream(csum);  
  •         //支持中文   
  •         zos.setEncoding("GBK");  
  •         BufferedOutputStream out = new BufferedOutputStream(zos);  
  •         //设置压缩包注释   
  •         zos.setComment(comment);  
  •         //启用压缩   
  •         zos.setMethod(ZipOutputStream.DEFLATED);  
  •         //压缩级别为最强压缩,但时间要花得多一点   
  •         zos.setLevel(Deflater.BEST_COMPRESSION);  
  •   
  •         File srcFile = new File(src);  
  •   
  •         if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) {  
  •             throw new FileNotFoundException(  
  •                     "File must exist and  ZIP file must have at least one entry.");  
  •         }  
  •         //获取压缩源所在父目录   
  •         src = src.replaceAll("\\\\", "/");  
  •         String prefixDir = null;  
  •         if (srcFile.isFile()) {  
  •             prefixDir = src.substring(0, src.lastIndexOf("/") + 1);  
  •         } else {  
  •             prefixDir = (src.replaceAll("/$", "") + "/");  
  •         }  
  •   
  •         //如果不是根目录   
  •         if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) {  
  •             prefixDir = prefixDir.replaceAll("[^/]+/$", "");  
  •         }  
  •   
  •         //开始压缩   
  •         writeRecursive(zos, out, srcFile, prefixDir);  
  •   
  •         out.close();  
  •         // 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用  
  •         System.out.println("Checksum: " + csum.getChecksum().getValue());  
  •         BufferedInputStream bi;  
  •     }  
  •   
  •     /**
  •      * 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的
  •      * java.util.zip.ZipFile 使用方式是一新的,只不过多了设置编码方式的
  •      * 接口。
  •      *  
  •      * 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile
  •      * 来读取压缩文件。
  •      * @param archive 压缩包路径
  •      * @param decompressDir 解压路径
  •      * @throws IOException
  •      * @throws FileNotFoundException
  •      * @throws ZipException
  •      */  
  •     public static void readByApacheZipFile(String archive, String decompressDir)  
  •             throws IOException, FileNotFoundException, ZipException {  
  •         BufferedInputStream bi;  
  •   
  •         ZipFile zf = new ZipFile(archive, "GBK");//支持中文  
  •   
  •         Enumeration e = zf.getEntries();  
  •         while (e.hasMoreElements()) {  
  •             ZipEntry ze2 = (ZipEntry) e.nextElement();  
  •             String entryName = ze2.getName();  
  •             String path = decompressDir + "/" + entryName;  
  •             if (ze2.isDirectory()) {  
  •                 System.out.println("正在创建解压目录 - " + entryName);  
  •                 File decompressDirFile = new File(path);  
  •                 if (!decompressDirFile.exists()) {  
  •                     decompressDirFile.mkdirs();  
  •                 }  
  •             } else {  
  •                 System.out.println("正在创建解压文件 - " + entryName);  
  •                 String fileDir = path.substring(0, path.lastIndexOf("/"));  
  •                 File fileDirFile = new File(fileDir);  
  •                 if (!fileDirFile.exists()) {  
  •                     fileDirFile.mkdirs();  
  •                 }  
  •                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(  
  •                         decompressDir + "/" + entryName));  
  •   
  •                 bi = new BufferedInputStream(zf.getInputStream(ze2));  
  •                 byte[] readContent = new byte[1024];  
  •                 int readCount = bi.read(readContent);  
  •                 while (readCount != -1) {  
  •                     bos.write(readContent, 0, readCount);  
  •                     readCount = bi.read(readContent);  
  •                 }  
  •                 bos.close();  
  •             }  
  •         }  
  •         zf.close();  
  •     }  
  •   
  •     /**
  •      * 使用 java api 中的 ZipInputStream 类解压文件,但如果压缩时采用了
  •      * org.apache.tools.zip.ZipOutputStream时,而不是 java 类库中的
  •      * java.util.zip.ZipOutputStream时,该方法不能使用,原因就是编码方
  •      * 式不一致导致,运行时会抛如下异常:
  •      * java.lang.IllegalArgumentException
  •      * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290)
  •      *  
  •      * 当然,如果压缩包使用的是java类库的java.util.zip.ZipOutputStream
  •      * 压缩而成是不会有问题的,但它不支持中文
  •      *  
  •      * @param archive 压缩包路径
  •      * @param decompressDir 解压路径
  •      * @throws FileNotFoundException
  •      * @throws IOException
  •      */  
  •     public static void readByZipInputStream(String archive, String decompressDir)  
  •             throws FileNotFoundException, IOException {  
  •         BufferedInputStream bi;  
  •         //----解压文件(ZIP文件的解压缩实质上就是从输入流中读取数据):  
  •         System.out.println("开始读压缩文件");  
  •   
  •         FileInputStream fi = new FileInputStream(archive);  
  •         CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32());  
  •         ZipInputStream in2 = new ZipInputStream(csumi);  
  •         bi = new BufferedInputStream(in2);  
  •         java.util.zip.ZipEntry ze;//压缩文件条目  
  •         //遍历压缩包中的文件条目   
  •         while ((ze = in2.getNextEntry()) != null) {  
  •             String entryName = ze.getName();  
  •             if (ze.isDirectory()) {  
  •                 System.out.println("正在创建解压目录 - " + entryName);  
  •                 File decompressDirFile = new File(decompressDir + "/" + entryName);  
  •                 if (!decompressDirFile.exists()) {  
  •                     decompressDirFile.mkdirs();  
  •                 }  
  •             } else {  
  •                 System.out.println("正在创建解压文件 - " + entryName);  
  •                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(  
  •                         decompressDir + "/" + entryName));  
  •                 byte[] buffer = new byte[1024];  
  •                 int readCount = bi.read(buffer);  
  •   
  •                 while (readCount != -1) {  
  •                     bos.write(buffer, 0, readCount);  
  •                     readCount = bi.read(buffer);  
  •                 }  
  •                 bos.close();  
  •             }  
  •         }  
  •         bi.close();  
  •         System.out.println("Checksum: " + csumi.getChecksum().getValue());  
  •     }  
  •   
  •     /**
  •      * 递归压缩
  •      *  
  •      * 使用 org.apache.tools.zip.ZipOutputStream 类进行压缩,它的好处就是支持中文路径,
  •      * 而Java类库中的 java.util.zip.ZipOutputStream 压缩中文文件名时压缩包会出现乱码。
  •      * 使用 apache 中的这个类与 java 类库中的用法是一新的,只是能设置编码方式了。
  •      *   
  •      * @param zos
  •      * @param bo
  •      * @param srcFile
  •      * @param prefixDir
  •      * @throws IOException
  •      * @throws FileNotFoundException
  •      */  
  •     private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,  
  •             File srcFile, String prefixDir) throws IOException, FileNotFoundException {  
  •         ZipEntry zipEntry;  
  •   
  •         String filePath = srcFile.getAbsolutePath().replaceAll("\\\\", "/").replaceAll(  
  •                 "//", "/");  
  •         if (srcFile.isDirectory()) {  
  •             filePath = filePath.replaceAll("/$", "") + "/";  
  •         }  
  •         String entryName = filePath.replace(prefixDir, "").replaceAll("/$", "");  
  •         if (srcFile.isDirectory()) {  
  •             if (!"".equals(entryName)) {  
  •                 System.out.println("正在创建目录 - " + srcFile.getAbsolutePath()  
  •                         + "  entryName=" + entryName);  
  •   
  •                 //如果是目录,则需要在写目录后面加上 /   
  •                 zipEntry = new ZipEntry(entryName + "/");  
  •                 zos.putNextEntry(zipEntry);  
  •             }  
  •   
  •             File srcFiles[] = srcFile.listFiles();  
  •             for (int i = 0; i < srcFiles.length; i&#43;&#43;) {  
  •                 writeRecursive(zos, bo, srcFiles, prefixDir);  
  •             }  
  •         } else {  
  •             System.out.println(&quot;正在写文件 - &quot; &#43; srcFile.getAbsolutePath() &#43; &quot;  entryName=&quot;  
  •                     &#43; entryName);  
  •             BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));  
  •   
  •             //开始写入新的ZIP文件条目并将流定位到条目数据的开始处   
  •             zipEntry = new ZipEntry(entryName);  
  •             zos.putNextEntry(zipEntry);  
  •             byte[] buffer = new byte[1024];  
  •             int readCount = bi.read(buffer);  
  •   
  •             while (readCount != -1) {  
  •                 bo.write(buffer, 0, readCount);  
  •                 readCount = bi.read(buffer);  
  •             }  
  •             //注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不  
  •             //然可能有的内容就会存入到后面条目中去了  
  •             bo.flush();  
  •             //文件读完后关闭   
  •             bi.close();  
  •         }  
  •     }  
  • }  
转载:http://write.blog.iyunv.com/postedit

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-138911-1-1.html 上篇帖子: 利用 org.apache.commons.io.FileUtils快速读写文件 下篇帖子: Apache, Tomcat, IIS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表