package cc.itzone.gln.zip;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author guoxl
* @date 2009-09-05
* @version 1.0
*/
public
class FileUtils {
public
static
void main(String[] args) {
// 待壓縮的文件名列表
List<String> lstFileNames =
new ArrayList<String>();
lstFileNames.add(
"D:\\wefLogs\\log.2009-08-09-14.log");
lstFileNames.add(
"D:\\wefLogs\\log.2009-08-10-09.log");
lstFileNames.add(
"D:\\wefLogs\\log.2009-08-10-11.log");
// 輸出的壓縮文件名
String strOutputZipFileName =
"D:\\test";
FileUtils fileUtils =
new FileUtils();
fileUtils.zipFile(lstFileNames, strOutputZipFileName);
}
public
void zipFile(List<String> lstFileNames, String strOutputZipFileName){
try {
FileOutputStream f =
new FileOutputStream(strOutputZipFileName +
".zip");
CheckedOutputStream ch =
new CheckedOutputStream(f,
new CRC32());
ZipOutputStream out =
new ZipOutputStream(
new BufferedOutputStream(ch));
for (String strFileName : lstFileNames) {
BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(strFileName),
"ISO8859_1"));
int c;
out.putNextEntry(
new ZipEntry(strFileName.substring(strFileName.lastIndexOf(File.separator))));
while ((c = in.read()) != -1)
out.write(c);
in.close();
}
out.close();
}
catch (FileNotFoundException e) {
// TODO
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
// TODO
e.printStackTrace();
}
catch (IOException e) {
// TODO e.printStackTrace(); } } }