Zip压缩与解压缩

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.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Test {
	public static void main(String[] args) {
		try {
			ZipOutputStream zop = new ZipOutputStream(new FileOutputStream("d:/g.zip"));
			String [] strArray = {"good.txt","ui.txt"};
			for (int i = 0; i < strArray.length; i++) {
				String fileName = strArray[i];
				String filePath = "d:/";
				zipFile(zop, fileName, filePath);
			}
			zop.close();
			
			
			FileInputStream fis = new FileInputStream("d:/g.zip");
			ZipInputStream zis = new ZipInputStream(fis);
			BufferedReader bis = new BufferedReader(new InputStreamReader(zis));
			ZipEntry ze ;
			while((ze = zis.getNextEntry())!=null){
				System.out.println("read File:"+ze);
				String str ;
				while((str = bis.readLine())!=null){
					System.out.println(str);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void zipFile(ZipOutputStream out,String name,String filePath){
		try {
			File file = new File(filePath + name);
			out.putNextEntry(new ZipEntry(name));
			BufferedReader br = new BufferedReader(new FileReader(file));
			int c ;
			while((c = br.read())!=-1){
				out.write(c);
			}
			out.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 上例子演示了添加文件到zip文件及读取

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