자바 파일 을 지정 한 위치 로 압축 합 니 다.

2407 단어 자바 배경
package cn.com.cis.acic.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class ZIPUtil {
	/***
	 * fileList                    
	 * zipFileName            : date/file/2019/12/2101001.zip
	 * */
	public static void main(String[] args) {
		ArrayList arrayList=new ArrayList();
		arrayList.add("D:\\zip/21000.xlsx");
		arrayList.add("D:\\zip/21004007717-File.xlsx");
		ZIPUtil.createZipFile(arrayList, "D:\\zip/aa.zip");
		
	}
	
    public static String createZipFile(ArrayList fileList,
        String zipFileName) {
        if ((fileList == null) || (fileList.size() == 0)) {
            return null;
        }

        //      File
        File zipFile = new File(zipFileName);
        //   ZIP 
        ZipOutputStream out = null;
        try {
            //  ZIP   
            out = new ZipOutputStream(new FileOutputStream(zipFile));
            //          
            for (int i = 0; i < fileList.size(); i++) {
                //      
                File inFile = new File(fileList.get(i));
                if (inFile.exists()) {
                    //  ZipEntry  
                    ZipEntry entry = new ZipEntry(inFile.getName());
                    //  ZIP     
                    out.putNextEntry(entry);
                    int len = 0;
                    //  
                    byte[] buffer = new byte[1024];
                    //  FileInputStream   
                    FileInputStream fis;
                    fis = new FileInputStream(inFile);
                    while ((len = fis.read(buffer)) > 0) {
                        out.write(buffer, 0, len);
                        out.flush();
                    }
                    //  closeEntry
                    out.closeEntry();
                    //  FileInputStream
                    fis.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //    ZIP 
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return zipFileName;
    }
}

좋은 웹페이지 즐겨찾기