`

利用Java实现压缩与解压缩(zip、gzip)支持中文路径

    博客分类:
  • Java
阅读更多

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代码 复制代码 收藏代码
  1. import java.io.BufferedOutputStream;   
  2. import java.io.BufferedReader;   
  3. import java.io.FileInputStream;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6. import java.io.InputStreamReader;   
  7. import java.util.zip.GZIPInputStream;   
  8. import java.util.zip.GZIPOutputStream;   
  9.   
  10. public class GZIPcompress {   
  11.     public static void main(String[] args) throws IOException {   
  12.         //做准备压缩一个字符文件,注,这里的字符文件要是GBK编码方式的   
  13.         BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(   
  14.                 "e:/tmp/source.txt"), "GBK"));   
  15.         //使用GZIPOutputStream包装OutputStream流,使其具体压缩特性,最后会生成test.txt.gz压缩包   
  16.         //并且里面有一个名为test.txt的文件   
  17.         BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(   
  18.                 new FileOutputStream("test.txt.gz")));   
  19.         System.out.println("开始写压缩文件...");   
  20.         int c;   
  21.         while ((c = in.read()) != -1) {   
  22.   
  23.             /*   
  24.              * 注,这里是压缩一个字符文件,前面是以字符流来读的,不能直接存入c,因为c已是Unicode  
  25.              * 码,这样会丢掉信息的(当然本身编码格式就不对),所以这里要以GBK来解后再存入。  
  26.              */  
  27.             out.write(String.valueOf((char) c).getBytes("GBK"));   
  28.         }   
  29.         in.close();   
  30.         out.close();   
  31.         System.out.println("开始读压缩文件...");   
  32.         //使用GZIPInputStream包装InputStream流,使其具有解压特性   
  33.         BufferedReader in2 = new BufferedReader(new InputStreamReader(   
  34.                 new GZIPInputStream(new FileInputStream("test.txt.gz")), "GBK"));   
  35.         String s;   
  36.         //读取压缩文件里的内容   
  37.         while ((s = in2.readLine()) != null) {   
  38.             System.out.println(s);   
  39.         }   
  40.         in2.close();   
  41.     }   
  42. }  
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPcompress {
	public static void main(String[] args) throws IOException {
		//做准备压缩一个字符文件,注,这里的字符文件要是GBK编码方式的
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(
				"e:/tmp/source.txt"), "GBK"));
		//使用GZIPOutputStream包装OutputStream流,使其具体压缩特性,最后会生成test.txt.gz压缩包
		//并且里面有一个名为test.txt的文件
		BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(
				new FileOutputStream("test.txt.gz")));
		System.out.println("开始写压缩文件...");
		int c;
		while ((c = in.read()) != -1) {

			/* 
			 * 注,这里是压缩一个字符文件,前面是以字符流来读的,不能直接存入c,因为c已是Unicode
			 * 码,这样会丢掉信息的(当然本身编码格式就不对),所以这里要以GBK来解后再存入。
			 */
			out.write(String.valueOf((char) c).getBytes("GBK"));
		}
		in.close();
		out.close();
		System.out.println("开始读压缩文件...");
		//使用GZIPInputStream包装InputStream流,使其具有解压特性
		BufferedReader in2 = new BufferedReader(new InputStreamReader(
				new GZIPInputStream(new FileInputStream("test.txt.gz")), "GBK"));
		String s;
		//读取压缩文件里的内容
		while ((s = in2.readLine()) != null) {
			System.out.println(s);
		}
		in2.close();
	}
}

使用Zip进行多个文件压缩

Java对Zip格式类库支持得比较全面,得用它可以把多个文件压缩成一个压缩包。这个类库使用的是标准Zip格式,所以能与很多的压缩工具兼容。

ZipOutputStream类有设置压缩方法以及在压缩方式下使用的压缩级别,zipOutputStream.setMethod(int method)设置用于条目的默认压缩方法。只要没有为单个 ZIP 文件条目指定压缩方法,就使用ZipOutputStream所设置的压缩方法来存储,默认值为 ZipOutputStream.DEFLATED(表示进行压缩存储),还可以设置成STORED(表示仅打包归档存储)。ZipOutputStream在设置了压缩方法为DEFLATED后,我们还可以进一步使用setLevel(int level)方法来设置压缩级别,压缩级别值为0-9共10个级别(值越大,表示压缩越利害),默认为Deflater.DEFAULT_COMPRESSION=-1。当然我们也可以通过条目ZipEntry的setMethod方法为单个条件设置压缩方法。

类ZipEntry描述了存储在ZIP文件中的压缩文件。类中包含有多种方法可以用来设置和获得ZIP条目的信息。类ZipEntry是被ZipFile[zipFile.getInputStream(ZipEntry entry)]和ZipInputStream使用来读取ZIP文件,ZipOutputStream来写入ZIP文件的。有以下这些有用的方法:getName()返回条目名称、isDirectory()如果为目录条目,则返回 true(目录条目定义为其名称以 '/' 结尾的条目)、setMethod(int method) 设置条目的压缩方法,可以为 ZipOutputStream.STORED 或 ZipOutputStream .DEFLATED。

 

下面实例我们使用了apache的zip工具包(所在包为ant.jar ),因为java类型自带的不支持中文路径,不过两者使用的方式是一样的,只是apache压缩工具多了设置编码方式的接口,其他基本上是一样的。另外,如果使用org.apache.tools.zip.ZipOutputStream来压缩的话,我们只能使用org.apache.tools.zip.ZipEntry来解压,而不能使用java.util.zip.ZipInputStream来解压读取了,当然apache并未提供ZipInputStream类。

 

Java代码 复制代码 收藏代码
  1. import java.io.BufferedInputStream;   
  2. import java.io.BufferedOutputStream;   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.FileNotFoundException;   
  6. import java.io.FileOutputStream;   
  7. import java.io.IOException;   
  8. import java.util.Enumeration;   
  9. import java.util.zip.CRC32;   
  10. import java.util.zip.CheckedInputStream;   
  11. import java.util.zip.CheckedOutputStream;   
  12. import java.util.zip.Deflater;   
  13. import java.util.zip.ZipException;   
  14. import java.util.zip.ZipInputStream;   
  15.   
  16. import org.apache.tools.zip.ZipEntry;   
  17. import org.apache.tools.zip.ZipFile;   
  18. import org.apache.tools.zip.ZipOutputStream;   
  19.   
  20. /**  
  21.  *   
  22.  * 提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径  
  23.  *   
  24.  * @author jzj  
  25.  */  
  26. public class ZipCompress {   
  27.   
  28.     private static boolean isCreateSrcDir = true;//是否创建源目录   
  29.   
  30.     /**  
  31.      * @param args  
  32.      * @throws IOException  
  33.      */  
  34.     public static void main(String[] args) throws IOException {   
  35.         String src = "m:/新建文本文档.txt";//指定压缩源,可以是目录或文件   
  36.         String decompressDir = "e:/tmp/decompress";//解压路径   
  37.         String archive = "e:/tmp/test.zip";//压缩包路径   
  38.         String comment = "Java Zip 测试.";//压缩包注释   
  39.   
  40.         //----压缩文件或目录   
  41.         writeByApacheZipOutputStream(src, archive, comment);   
  42.   
  43.         /*  
  44.          * 读压缩文件,注释掉,因为使用的是apache的压缩类,所以使用java类库中  
  45.          * 解压类时出错,这里不能运行  
  46.          */  
  47.         //readByZipInputStream();   
  48.         //----使用apace ZipFile读取压缩文件   
  49.         readByApacheZipFile(archive, decompressDir);   
  50.     }   
  51.   
  52.     public static void writeByApacheZipOutputStream(String src, String archive,   
  53.             String comment) throws FileNotFoundException, IOException {   
  54.         //----压缩文件:   
  55.         FileOutputStream f = new FileOutputStream(archive);   
  56.         //使用指定校验和创建输出流   
  57.         CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());   
  58.   
  59.         ZipOutputStream zos = new ZipOutputStream(csum);   
  60.         //支持中文   
  61.         zos.setEncoding("GBK");   
  62.         BufferedOutputStream out = new BufferedOutputStream(zos);   
  63.         //设置压缩包注释   
  64.         zos.setComment(comment);   
  65.         //启用压缩   
  66.         zos.setMethod(ZipOutputStream.DEFLATED);   
  67.         //压缩级别为最强压缩,但时间要花得多一点   
  68.         zos.setLevel(Deflater.BEST_COMPRESSION);   
  69.   
  70.         File srcFile = new File(src);   
  71.   
  72.         if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) {   
  73.             throw new FileNotFoundException(   
  74.                     "File must exist and  ZIP file must have at least one entry.");   
  75.         }   
  76.         //获取压缩源所在父目录   
  77.         src = src.replaceAll("\\\\", "/");   
  78.         String prefixDir = null;   
  79.         if (srcFile.isFile()) {   
  80.             prefixDir = src.substring(0, src.lastIndexOf("/") + 1);   
  81.         } else {   
  82.             prefixDir = (src.replaceAll("/$""") + "/");   
  83.         }   
  84.   
  85.         //如果不是根目录   
  86.         if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) {   
  87.             prefixDir = prefixDir.replaceAll("[^/]+/$""");   
  88.         }   
  89.   
  90.         //开始压缩   
  91.         writeRecursive(zos, out, srcFile, prefixDir);   
  92.   
  93.         out.close();   
  94.         // 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用   
  95.         System.out.println("Checksum: " + csum.getChecksum().getValue());   
  96.         BufferedInputStream bi;   
  97.     }   
  98.   
  99.     /**  
  100.      * 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的  
  101.      * java.util.zip.ZipFile 使用方式是一新的,只不过多了设置编码方式的  
  102.      * 接口。  
  103.      *   
  104.      * 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile  
  105.      * 来读取压缩文件。  
  106.      * @param archive 压缩包路径  
  107.      * @param decompressDir 解压路径  
  108.      * @throws IOException  
  109.      * @throws FileNotFoundException  
  110.      * @throws ZipException  
  111.      */  
  112.     public static void readByApacheZipFile(String archive, String decompressDir)   
  113.             throws IOException, FileNotFoundException, ZipException {   
  114.         BufferedInputStream bi;   
  115.   
  116.         ZipFile zf = new ZipFile(archive, "GBK");//支持中文   
  117.   
  118.         Enumeration e = zf.getEntries();   
  119.         while (e.hasMoreElements()) {   
  120.             ZipEntry ze2 = (ZipEntry) e.nextElement();   
  121.             String entryName = ze2.getName();   
  122.             String path = decompressDir + "/" + entryName;   
  123.             if (ze2.isDirectory()) {   
  124.                 System.out.println("正在创建解压目录 - " + entryName);   
  125.                 File decompressDirFile = new File(path);   
  126.                 if (!decompressDirFile.exists()) {   
  127.                     decompressDirFile.mkdirs();   
  128.                 }   
  129.             } else {   
  130.                 System.out.println("正在创建解压文件 - " + entryName);   
  131.                 String fileDir = path.substring(0, path.lastIndexOf("/"));   
  132.                 File fileDirFile = new File(fileDir);   
  133.                 if (!fileDirFile.exists()) {   
  134.                     fileDirFile.mkdirs();   
  135.                 }   
  136.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(   
  137.                         decompressDir + "/" + entryName));   
  138.   
  139.                 bi = new BufferedInputStream(zf.getInputStream(ze2));   
  140.                 byte[] readContent = new byte[1024];   
  141.                 int readCount = bi.read(readContent);   
  142.                 while (readCount != -1) {   
  143.                     bos.write(readContent, 0, readCount);   
  144.                     readCount = bi.read(readContent);   
  145.                 }   
  146.                 bos.close();   
  147.             }   
  148.         }   
  149.         zf.close();   
  150.     }   
  151.   
  152.     /**  
  153.      * 使用 java api 中的 ZipInputStream 类解压文件,但如果压缩时采用了  
  154.      * org.apache.tools.zip.ZipOutputStream时,而不是 java 类库中的  
  155.      * java.util.zip.ZipOutputStream时,该方法不能使用,原因就是编码方  
  156.      * 式不一致导致,运行时会抛如下异常:  
  157.      * java.lang.IllegalArgumentException  
  158.      * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290)  
  159.      *   
  160.      * 当然,如果压缩包使用的是java类库的java.util.zip.ZipOutputStream  
  161.      * 压缩而成是不会有问题的,但它不支持中文  
  162.      *   
  163.      * @param archive 压缩包路径  
  164.      * @param decompressDir 解压路径  
  165.      * @throws FileNotFoundException  
  166.      * @throws IOException  
  167.      */  
  168.     public static void readByZipInputStream(String archive, String decompressDir)   
  169.             throws FileNotFoundException, IOException {   
  170.         BufferedInputStream bi;   
  171.         //----解压文件(ZIP文件的解压缩实质上就是从输入流中读取数据):   
  172.         System.out.println("开始读压缩文件");   
  173.   
  174.         FileInputStream fi = new FileInputStream(archive);   
  175.         CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32());   
  176.         ZipInputStream in2 = new ZipInputStream(csumi);   
  177.         bi = new BufferedInputStream(in2);   
  178.         java.util.zip.ZipEntry ze;//压缩文件条目   
  179.         //遍历压缩包中的文件条目   
  180.         while ((ze = in2.getNextEntry()) != null) {   
  181.             String entryName = ze.getName();   
  182.             if (ze.isDirectory()) {   
  183.                 System.out.println("正在创建解压目录 - " + entryName);   
  184.                 File decompressDirFile = new File(decompressDir + "/" + entryName);   
  185.                 if (!decompressDirFile.exists()) {   
  186.                     decompressDirFile.mkdirs();   
  187.                 }   
  188.             } else {   
  189.                 System.out.println("正在创建解压文件 - " + entryName);   
  190.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(   
  191.                         decompressDir + "/" + entryName));   
  192.                 byte[] buffer = new byte[1024];   
  193.                 int readCount = bi.read(buffer);   
  194.   
  195.                 while (readCount != -1) {   
  196.                     bos.write(buffer, 0, readCount);   
  197.                     readCount = bi.read(buffer);   
  198.                 }   
  199.                 bos.close();   
  200.             }   
  201.         }   
  202.         bi.close();   
  203.         System.out.println("Checksum: " + csumi.getChecksum().getValue());   
  204.     }   
  205.   
  206.     /**  
  207.      * 递归压缩  
  208.      *   
  209.      * 使用 org.apache.tools.zip.ZipOutputStream 类进行压缩,它的好处就是支持中文路径,  
  210.      * 而Java类库中的 java.util.zip.ZipOutputStream 压缩中文文件名时压缩包会出现乱码。  
  211.      * 使用 apache 中的这个类与 java 类库中的用法是一新的,只是能设置编码方式了。  
  212.      *    
  213.      * @param zos  
  214.      * @param bo  
  215.      * @param srcFile  
  216.      * @param prefixDir  
  217.      * @throws IOException  
  218.      * @throws FileNotFoundException  
  219.      */  
  220.     private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,   
  221.             File srcFile, String prefixDir) throws IOException, FileNotFoundException {   
  222.         ZipEntry zipEntry;   
  223.   
  224.         String filePath = srcFile.getAbsolutePath().replaceAll("\\\\", "/").replaceAll(   
  225.                 "//""/");   
  226.         if (srcFile.isDirectory()) {   
  227.             filePath = filePath.replaceAll("/$""") + "/";   
  228.         }   
  229.         String entryName = filePath.replace(prefixDir, "").replaceAll("/$""");   
  230.         if (srcFile.isDirectory()) {   
  231.             if (!"".equals(entryName)) {   
  232.                 System.out.println("正在创建目录 - " + srcFile.getAbsolutePath()   
  233.                         + "  entryName=" + entryName);   
  234.   
  235.                 //如果是目录,则需要在写目录后面加上 /    
  236.                 zipEntry = new ZipEntry(entryName + "/");   
  237.                 zos.putNextEntry(zipEntry);   
  238.             }   
  239.   
  240.             File srcFiles[] = srcFile.listFiles();   
  241.             for (int i = 0; i < srcFiles.length; i++) {   
  242.                 writeRecursive(zos, bo, srcFiles[i], prefixDir);   
  243.             }   
  244.         } else {   
  245.             System.out.println("正在写文件 - " + srcFile.getAbsolutePath() + "  entryName="  
  246.                     + entryName);   
  247.             BufferedInputStream bi = new</span
    分享到:
    评论
    发表评论

    文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics