- package com.tw.file.util;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
-
-
-
-
-
-
- public class ZipManager {
- private static ZipManager instance;
- private static final int BUFF_SIZE = 1024 * 1024;
-
-
-
-
-
- public synchronized static ZipManager getInstance(){
- if( instance == null ){
- instance = new ZipManager();
- }
- return instance;
- }
-
-
-
-
-
-
-
-
- public static void batchZipFiles(Collection<File> resFileList, File zipFile) throws IOException {
-
- ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
- for (File resFile : resFileList) {
- byte buffer[] = new byte[BUFF_SIZE];
- BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
-
- zipout.putNextEntry(new ZipEntry(resFile.getName()));
- int realLength;
-
- while ((realLength = in.read(buffer)) != -1) {
- zipout.write(buffer, 0, realLength);
- }
- in.close();
- zipout.flush();
- zipout.closeEntry();
- }
- zipout.close();
- }
-
-
-
-
-
-
- public static void main(String[] args) {
- Collection<File> resFileList = new ArrayList<File>();
- resFileList.add(new File("E:\\im\\deps_last.xml"));
-
-
- File zipFile = new File("e:\\im\\deps_last.zip");
- try {
- batchZipFiles(resFileList,zipFile);
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
-
- }