Java Zip多文件压缩和 GZIP压缩

 
  1. /**  
  2.  * 多文件压缩  
  3.  *   
  4.  * @author Administrator  
  5.  *   
  6.  */  
  7. public class ZipCompress {   
  8.   
  9.     public static void main(String args[]) {   
  10.         String[] filepaths = { "D:\\zip1.txt""D:\\zip2.txt" };   
  11.         try {   
  12.             FileOutputStream f = new FileOutputStream("D://test.zip");   
  13.             // 输出校验流,采用Adler32更快   
  14.             CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());   
  15.             //创建压缩输出流   
  16.             ZipOutputStream zos = new ZipOutputStream(csum);   
  17.             BufferedOutputStream out = new BufferedOutputStream(zos);   
  18.             //设置Zip文件注释   
  19.             zos.setComment("A test of java Zipping");   
  20.             for (String s : filepaths) {   
  21.                 System.out.println("Writing file " + s);   
  22.                 //针对单个文件建立读取流   
  23.                 BufferedReader bin = new BufferedReader(new FileReader(s));   
  24.                 //ZipEntry ZIP 文件条目   
  25.                 //putNextEntry 写入新条目,并定位到新条目开始处   
  26.                 zos.putNextEntry(new ZipEntry(s));   
  27.                 int c;   
  28.                 while ((c = bin.read()) != -1) {   
  29.                     out.write(c);   
  30.                 }   
  31.                 bin.close();   
  32.                 out.flush();   
  33.             }   
  34.             out.close();   
  35.             FileInputStream fi = new FileInputStream("D://test.zip");   
  36.             CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());   
  37.             ZipInputStream in2 = new ZipInputStream(csumi);   
  38.             BufferedInputStream bis = new BufferedInputStream(in2);   
  39.             ZipEntry ze;   
  40.             while ((ze = in2.getNextEntry()) != null) {   
  41.                 System.out.println("Reader File " + ze);   
  42.                 int x;   
  43.                 while ((x = bis.read()) != -1)   
  44.                     System.out.println(x);   
  45.             }   
  46.             //利用ZipFile解压压缩文件   
  47.             ZipFile zf = new ZipFile("D:\\test.zip");   
  48.             Enumeration e = zf.entries();   
  49.             while(e.hasMoreElements()){   
  50.                 ZipEntry ze2 = (ZipEntry) e.nextElement();   
  51.                 System.out.println("File name : "+ze2);   
  52.             }   
  53.         } catch (FileNotFoundException e) {   
  54.             e.printStackTrace();   
  55.         } catch (IOException e) {   
  56.             e.printStackTrace();   
  57.         }   
  58.     }   
  59. }  
/**
 * 多文件压缩
 * 
 * @author Administrator
 * 
 */
public class ZipCompress {

	public static void main(String args[]) {
		String[] filepaths = { "D:\\zip1.txt", "D:\\zip2.txt" };
		try {
			FileOutputStream f = new FileOutputStream("D://test.zip");
			// 输出校验流,采用Adler32更快
			CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
			//创建压缩输出流
			ZipOutputStream zos = new ZipOutputStream(csum);
			BufferedOutputStream out = new BufferedOutputStream(zos);
			//设置Zip文件注释
			zos.setComment("A test of java Zipping");
			for (String s : filepaths) {
				System.out.println("Writing file " + s);
				//针对单个文件建立读取流
				BufferedReader bin = new BufferedReader(new FileReader(s));
				//ZipEntry ZIP 文件条目
				//putNextEntry 写入新条目,并定位到新条目开始处
				zos.putNextEntry(new ZipEntry(s));
				int c;
				while ((c = bin.read()) != -1) {
					out.write(c);
				}
				bin.close();
				out.flush();
			}
			out.close();
			FileInputStream fi = new FileInputStream("D://test.zip");
			CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
			ZipInputStream in2 = new ZipInputStream(csumi);
			BufferedInputStream bis = new BufferedInputStream(in2);
			ZipEntry ze;
			while ((ze = in2.getNextEntry()) != null) {
				System.out.println("Reader File " + ze);
				int x;
				while ((x = bis.read()) != -1)
					System.out.println(x);
			}
			//利用ZipFile解压压缩文件
			ZipFile zf = new ZipFile("D:\\test.zip");
			Enumeration e = zf.entries();
			while(e.hasMoreElements()){
				ZipEntry ze2 = (ZipEntry) e.nextElement();
				System.out.println("File name : "+ze2);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

    GZIP压缩

 

   

Java代码 复制代码  收藏代码
  1. /**  
  2.  * 文件压缩  
  3.  * 把文件压缩成GZIP 单一的流数据 并不是互异的数据  
  4.  * GZIPOutputStream的使用  
  5.  * @author Administrator  
  6.  *   
  7.  */  
  8. public class GzipPcompress {   
  9.   
  10.     public static void main(String args[]) {   
  11.         try {   
  12.             BufferedReader in = new BufferedReader(new FileReader("D:\\gziptest.txt"));   
  13.   
  14.             BufferedOutputStream out = new BufferedOutputStream(   
  15.                     new GZIPOutputStream(new FileOutputStream("D:\\test.gz")));   
  16.             System.out.println("Writing file");   
  17.             int c;   
  18.             while ((c = in.read()) != -1) {   
  19.                 out.write(c);   
  20.             }   
  21.             in.close();   
  22.             out.close();   
  23.             System.out.println("Reading file");   
  24.             BufferedReader in2 = new BufferedReader(new InputStreamReader(   
  25.                     new GZIPInputStream(new FileInputStream("D:\\test.gz"))));   
  26.             String s;   
  27.             while((s=in2.readLine()) != null){   
  28.                 System.out.println(s);   
  29.             }   
  30.   
  31.         } catch (FileNotFoundException e) {   
  32.             e.printStackTrace();   
  33.         } catch (IOException e) {   
  34.             e.printStackTrace();   
  35.         }   
  36.     }   
  37. }  
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值