Article From:https://www.cnblogs.com/hua900822/p/9684068.html
package com.day1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Demo7 {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        
        copyFile();

    }

    private static void copyFile() throws Exception {
        
        System.out.println("Start copying ");
        
        File src=new File("f:/src");     //Source folder
        File dest=new File("f:/dest");  //Target folder
        
        
        File[] list = src.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                boolean b=true;
                if(name.endsWith(".zip"))
                {
                    b=false;
                }
                return b;
            }
        });
        
        InputStream in=null;
        OutputStream out=null;
        
        
        File zipFile=new File("f:/src/crm.zip");
        FileOutputStream zipOut=new FileOutputStream(zipFile);
        ZipOutputStream zip=new ZipOutputStream(zipOut);
        zip.setComment("liuxunTEST") ;    // Setting annotations
        for (File file : list) {
            
            File my=new File(dest,file.getName());
            
            in=new FileInputStream(file);
            out=new FileOutputStream(my);
            int len=-1;
            byte[] buffer=new byte[1024];
            
            //while((len=in.read(buffer)) != -1)
            //{
            //   out.write(buffer, 0, len);
            //}
            
            //Stored in the compressed package
            ZipEntry entry=new ZipEntry(file.getName());
            zip.putNextEntry(entry);
            while((len=in.read(buffer)) != -1)
            {
                zip.write(buffer, 0, len);
            }
            
            in.close();
            out.close();
        }
        zip.close();
        zipOut.close();

    }

}

 

Link of this Article: 02-zip file packaging

Leave a Reply

Your email address will not be published. Required fields are marked *