码迷,mamicode.com
首页 > 编程语言 > 详细

java压缩 GZIP进行简单压缩,ZIP进行多文件保存

时间:2017-05-19 13:32:52      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:bsp   out   iss   public   ipo   system   and   throw   ring   

java压缩  GZIP进行简单压缩,ZIP进行多文件保存

技术分享

package org.rui.io.compress;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
 * 用GZIP进行简单压缩
 * @author lenovo
 *
 */
public class GZIPcompress {
	static String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\compress\\";
	static String [] arg=new String[]{path+"GZIPcompress.java"};
	public static void main(String[] args) throws Exception {
		//in
		BufferedReader in=new BufferedReader(new FileReader(arg[0]));
		//out
		BufferedOutputStream out=new BufferedOutputStream(new GZIPOutputStream(
				new FileOutputStream("test.gz")
				));
		System.out.println("writing file");
		int c;
		while((c=in.read())!=-1)
		{
			out.write(c);
		}
		//close
		in.close();
		out.close();
		System.out.println("reading file================");
		BufferedReader br=new BufferedReader(
				new InputStreamReader(
						new GZIPInputStream(
								new FileInputStream("test.gz")
								)
						)
				);
		
		String s;
		while((s=br.readLine())!=null)
			System.out.println(s);
	}

}


技术分享

package org.rui.io.compress;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 用Zip进行多文件保存
 * @author lenovo
 *
 */
public class ZipCompress {
	//static String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\compress\\";
	static String path=new File("").getAbsolutePath()+"\\src\\org\\rui\\io\\compress\\";
	static String [] arg=new String[]{path+"GZIPcompress.java",path+"ZipCompress.java"};
	
	public static void main(String[] args) throws Exception {
		//out
		FileOutputStream f=new FileOutputStream("test.zip");
		CheckedOutputStream cos=new CheckedOutputStream(f,new Adler32());
		ZipOutputStream zos=new ZipOutputStream(cos);
		//out
		BufferedOutputStream out=new BufferedOutputStream(zos);
		zos.setComment("A test of java zipping");
		
		for(String s:arg)
		{
			System.out.println("writing file "+s);
			BufferedReader in =new BufferedReader(new FileReader(s));
			//
			zos.putNextEntry(new ZipEntry(s));
			int c;
			while((c=in.read())!=-1){
				out.write(c);
			}
			in.close();
			out.flush();
		}
		
		out.close();
		//checksum valid only after the file has been closed!
		System.out.println("reading file================");
		FileInputStream fi=new FileInputStream("test.zip");
		CheckedInputStream csumi=new CheckedInputStream(fi,new Adler32());
		ZipInputStream zis=new ZipInputStream(csumi);
		BufferedInputStream bis=new BufferedInputStream(zis);
		ZipEntry ze;
		while((ze=zis.getNextEntry())!=null)
		{
			System.out.println("Reading file "+ze);
			int x;
			while((x=bis.read())!=-1)
			{
				//System.out.print((char)x);
				System.out.write(x);
			}
		}
		
		//
		if(arg.length==1)
			System.out.println("checksum:"+csumi.getChecksum().getValue());
		bis.close();
		//alternative way to open and read zip files
		ZipFile zf=new ZipFile("test.zip");
		
		Enumeration e=zf.entries();
		while(e.hasMoreElements())
		{
			ZipEntry ze2=(ZipEntry) e.nextElement();
			System.out.println("file:"+ze2);
			//...and extract the data as before
		}
		/*if(arg.length==1)*/
		
	}

}


技术分享



技术分享





技术分享


java压缩 GZIP进行简单压缩,ZIP进行多文件保存

标签:bsp   out   iss   public   ipo   system   and   throw   ring   

原文地址:http://www.cnblogs.com/jzssuanfa/p/6877824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!