java编程思想 IO13 源码 文件解压缩

package com.io;
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPcompress {

	/**
	 * 用GZIP进行简单压缩
	 * 直接将输出流封装成GZIPOutputStream或ZIPOutputStream,并将输入流封装成
	 * GZIPInputStream或ZIPInputStream即可
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		if(args.length==0){
			System.err.println("Usage: \nGZIPcompress file\n"+
					"\t Uses GZIP compress to compress "+
					"the file to test.gz");
			System.exit(1);
		}
		BufferedReader in=new BufferedReader(new FileReader(args[0]));
		//GZIPOutputStream用于将数据压缩成GZIP文件格式
		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);
		}
		in.close();
		out.close();
		System.out.println("Reading file:");
		//GZIPInputStream用于解压缩GZIP文件格式的数据
		BufferedReader in2=new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));
		String s;
		while((s=in2.readLine())!=null){
			System.out.println(s);
		}
		in.close();
	}
	

}

package com.io;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
public class ZipCompress {

	/**
	 *用Zip进行多文件的保存
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//文件压缩
		FileOutputStream f=new FileOutputStream("test.zip");
		//public class CheckedOutputStreamextends FilterOutputStream
		//需要维护写入数据校验和的输出流。校验和可用于验证输出数据的完整性。 
		CheckedOutputStream csum=new CheckedOutputStream(f, new Adler32());
		ZipOutputStream zos=new ZipOutputStream(csum);
		BufferedOutputStream out=new BufferedOutputStream(zos);
		zos.setComment("A test of Java Zipping");//设置 ZIP 文件注释。 
		for(String arg:args){
			System.out.println("Writing file:"+arg);
			BufferedReader in=new BufferedReader(new FileReader(arg));
			//开始写入新的 ZIP 文件条目并将流定位到条目数据的开始处
			//ZipEntry是Java中进行压缩与解压缩的单位,它用来标记ZIP压缩文件中每个原始文件的入口
			ZipEntry zeItem=new ZipEntry(arg);
			System.out.println(zeItem.getCompressedSize()+"|"+zeItem.getTime()+"|"+zeItem.getSize());
			zos.putNextEntry(zeItem);
			int c;
			while((c=in.read())!=-1){
				out.write(c);
			}
			in.close();
			out.flush();
		}	
		out.close();
		System.out.println("Checksum: "+csum.getChecksum().getValue());
		//文件解压缩
		FileInputStream fi=new FileInputStream("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("Reading file "+ze);
			int x;
			while((x=bis.read())!=-1){
				System.out.print((char)x);
			}
			System.out.println();
		}
		if(args.length==1){
			System.out.println("Checksum: "+csum.getChecksum().getValue());		
		}
		bis.close();
		ZipFile zf=new ZipFile("test.zip");
		Enumeration e=zf.entries();
		while(e.hasMoreElements()){
			ZipEntry ze2=(ZipEntry)e.nextElement();
			System.out.println("File :"+ze2);
		}
		
		
	}

}

 

你可能感兴趣的:(java,编程,C++,c,C#)