java 多个文件打包zip

/**

 * 多个文件打包成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();

    }

}

 

你可能感兴趣的:(java)