/**
 * 多个文件打包成zip
 */
public class ZipDemo
{
    private static void create() throws Exception{
        String path="d:/demo.zip";
        ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(new File(path)));
        File[] files={new File("d:/1.doc"),new File("d:/2.doc")};
        byte [] buffer=new byte[1024];
        int len=-1;
        for(int i=0;i<files.length;i++){
            FileInputStream in=new FileInputStream(files[i]);
            zipOut.putNextEntry(new ZipEntry(files[i].getName()));
            while((len=in.read(buffer))!=-1){
               zipOut.write(buffer, 0, len);
            }
            zipOut.closeEntry();
            in.close();
        }
        zipOut.close();
        System.out.println("文件已经压缩成zip了"+path);
    }
    public static void main(String[] args) throws Exception
    {
        create();
    }
}

 

posted on 2015-04-24 10:11  ccasdaaammm  阅读(369)  评论(0编辑  收藏  举报