JAVA IO - 压缩流

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class ZipTest {
    public static void main(String args[]){
    	try(ZipOutputStream  zo = new ZipOutputStream(new FileOutputStream("hello.zip")); ZipInputStream  zi = new ZipInputStream(new FileInputStream("hello.zip")); ){
    		zo.putNextEntry(new ZipEntry("hello"));
    		zo.write("hello".getBytes());
    		zo.closeEntry();
    		zo.putNextEntry(new ZipEntry("world"));
    		zo.write("world".getBytes());
    		zo.closeEntry();
    		zo.putNextEntry(new ZipEntry("java"));
    		zo.write("java".getBytes());
    		zo.closeEntry();
    		zo.close();
    		byte[] bs = new byte[1024];
    		ZipEntry entry = null;
    		while((entry = zi.getNextEntry()) != null){
    			int len = -1;
                if((len = zi.read(bs)) != -1){
                	System.out.println(new String(bs, 0, len));
                }
    		}
    		
    		zi.close();
    		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
}




你可能感兴趣的:(ZipOutputStream,ZipInputStream)