Java壓縮和解壓文件工具類ZipUtil
用于服務器端打包文件的,將壓縮后的文件寫入到response輸出流即可實現在服務器端打包下載,支持多級目錄嵌套。
測試時可以先用ZipUtil.zip壓縮某個文件夾test,得到壓縮文件test.zip,然后將文件夾test刪除,用ZipUtil.unzip解壓test.zip即可還原。
PS:需要解決中文亂碼的朋友可以參考此處 http://log-cd.iteye.com/blog/585647
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.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream;/**
- 通過Java的Zip輸入輸出流實現壓縮和解壓文件
- @author liujiduo
*/ public final class ZipUtil {
private static byte[] buffer = new byte[1024 * 10];
private ZipUtil() {
// empty
}
/**
- 壓縮文件
- @param filePath
- 待壓縮的文件路徑
@return 壓縮后的文件 */ public static File zip(String filePath) { File target = null; File source = new File(filePath); if (source.exists()) {
// 壓縮文件名=源文件名.zip String zipName = source.getName() + ".zip"; target = new File(source.getParent(), zipName); if (target.exists()) { target.delete(); // 刪除舊的文件 } FileOutputStream fos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(target); zos = new ZipOutputStream(new BufferedOutputStream(fos)); // 添加對應的文件Entry addEntry("/", source, zos); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtil.closeQuietly(zos, fos); }
} return target; }
/**
- 掃描添加文件Entry
- @param base
- 基路徑
- @param source
- 源文件
- @param zos
- Zip文件輸出流
@throws IOException */ private static void addEntry(String base, File source, ZipOutputStream zos)
throws IOException {
// 按目錄分級,形如:/aaa/bbb.txt String entry = base + source.getName(); if (source.isDirectory()) {
for (File file : source.listFiles()) { // 遞歸列出目錄下的所有文件,添加文件Entry addEntry(entry + "/", file, zos); }
} else {
FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(source); bis = new BufferedInputStream(fis, buffer.length); int read = 0; zos.putNextEntry(new ZipEntry(entry)); while ((read = bis.read(buffer, 0, buffer.length)) != -1) { zos.write(buffer, 0, read); } zos.closeEntry(); } finally { IOUtil.closeQuietly(bis, fis); }
} }
/**
- 解壓文件
- @param filePath
壓縮文件路徑 */ public static void unzip(String filePath) { File source = new File(filePath); if (source.exists()) { ZipInputStream zis = null; BufferedOutputStream bos = null; try { zis = new ZipInputStream(new FileInputStream(source)); ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null && !entry.isDirectory()) { File target = new File(source.getParent(), entry.getName()); if (!target.getParentFile().exists()) {
// 創建文件父目錄 target.getParentFile().mkdirs();
} // 寫入文件 bos = new BufferedOutputStream(new FileOutputStream(target)); int read = 0; while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, read);
} bos.flush(); } zis.closeEntry(); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtil.closeQuietly(zis, bos); } } }
public static void main(String[] args) { String targetPath = "E:\Win7壁紙"; File file = ZipUtil.zip(targetPath); System.out.println(file); ZipUtil.unzip("F:\Win7壁紙.zip"); } }</pre>
import java.io.Closeable; import java.io.IOException;
/**
- IO流工具類
- @author liujiduo
*/ public class IOUtil { /**
- 關閉一個或多個流對象
- @param closeables
- 可關閉的流對象列表
@throws IOException */ public static void close(Closeable... closeables) throws IOException { if (closeables != null) {
for (Closeable closeable : closeables) { if (closeable != null) { closeable.close(); } }
} }
/**
- 關閉一個或多個流對象
- @param closeables
- 可關閉的流對象列表 */ public static void closeQuietly(Closeable... closeables) { try { close(closeables); } catch (IOException e) { // do nothing } }
}</pre>