Java中压缩多个文件

利用文件压缩 ZipOutputStream类

 

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.zip.ZipEntry;  
import java.util.zip.ZipOutputStream;  
 
public class ZipOutputStreamDemo1{  
    public static void main(String[] args) throws IOException{  
        File file = new File("d:" + File.separator + "hello.txt");  
        File zipFile = new File("d:" + File.separator + "hello.zip");  
        InputStream input = new FileInputStream(file);  
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(  
                zipFile));  
        zipOut.putNextEntry(new ZipEntry(file.getName()));  
        // 设置注释  
        zipOut.setComment("hello");  
        int temp = 0;  
        while((temp = input.read()) != -1){  
            zipOut.write(temp);  
        }  
        input.close();  
        zipOut.close();  
    }  
} 
 

你可能感兴趣的:(java,压缩)