Java創立ZIP緊縮文件的辦法。本站提示廣大學習愛好者:(Java創立ZIP緊縮文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java創立ZIP緊縮文件的辦法正文
本文實例講述了Java創立ZIP緊縮文件的辦法。分享給年夜家供年夜家參考。詳細以下:
這裡留意:建議應用org.apache.tools.zip.*包下相干類,不然能夠會湧現中文亂碼成績。
/**
* 緊縮文件夾
* @param sourceDIR 文件夾稱號(包括途徑)
* @param targetZipFile 生成zip文件名
* @author liuxiangwei
*/
public static void zipDIR(String sourceDIR, String targetZipFile) {
try {
FileOutputStream target = new FileOutputStream(targetZipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(target));
int BUFFER_SIZE = 1024;
byte buff[] = new byte[BUFFER_SIZE];
File dir = new File(sourceDIR);
if (!dir.isDirectory()) {
throw new IllegalArgumentException(sourceDIR+" is not a directory!");
}
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
BufferedInputStream origin = new BufferedInputStream(fi);
ZipEntry entry = new ZipEntry(files[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(buff)) != -1) {
out.write(buff, 0, count);
}
origin.close();
}
out.close();
} catch (IOException e) {
throw new MsgException("");
}
}
願望本文所述對年夜家的java法式設計有所贊助。