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

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进行多文件保存,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。