一、简述
解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如:
CheckedInputStream cis = new CheckedInputStream(new FileInputStream( srcFile), new CRC32()); ZipInputStream zis = new ZipInputStream(cis);
需要注意的是,在构建解压文件时,需要考虑目录的自动创建,这里通过递归方式逐层创建父目录,如下所示:
//当父目录不存在时,创建目录! private static void fileProber(File dirFile) { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { // 递归寻找上级目录 fileProber(parentFile); parentFile.mkdir(); } }
在压缩的时候,我们是将一个一个文件作为压缩添加项(ZipEntry)添加至压缩包中,解压缩就要将一个一个压缩项从压缩包中提取出来,如下所示:
private static void decompress(File destFile, ZipInputStream zis) throws Exception { ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { // 文件 String dir = destFile.getPath() + File.separator + entry.getName(); File dirFile = new File(dir); // 文件检查 fileProber(dirFile); if (entry.isDirectory()){ dirFile.mkdirs(); } else { decompressFile(dirFile, zis); } zis.closeEntry(); } }
最核心的解压缩实现,其实与压缩实现非常相似,代码如下所示:
/** * 文件解压缩 * * @param destFile * 目标文件 * @param zis * ZipInputStream * @throws Exception */ private static void decompressFile(File destFile, ZipInputStream zis) throws Exception { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); int count; byte data[] = new byte[BUFFER]; while ((count = zis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.close(); }
完整的例子:
package com.joyplus.test; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * * @author * */ public class zipFiletest2 { public static final String EXT = ".zip"; private static final String BASE_DIR = ""; private static final String PATH = File.separator; private static final int BUFFER = 1024; /** * 文件 解压缩 * * @param srcPath * 源文件路径 * * @throws Exception */ public static void decompress(String srcPath) throws Exception { File srcFile = new File(srcPath); decompress(srcFile); } /** * 解压缩 * * @param srcFile * @throws Exception */ public static void decompress(File srcFile) throws Exception { String basePath = srcFile.getParent(); decompress(srcFile, basePath); } /** * 解压缩 * * @param srcFile * @param destFile * @throws Exception */ public static void decompress(File srcFile, File destFile) throws Exception { CheckedInputStream cis = new CheckedInputStream(new FileInputStream( srcFile), new CRC32()); ZipInputStream zis = new ZipInputStream(cis); decompress(destFile, zis); zis.close(); } /** * 解压缩 * * @param srcFile * @param destPath * @throws Exception */ public static void decompress(File srcFile, String destPath) throws Exception { decompress(srcFile, new File(destPath)); } /** * 文件 解压缩 * * @param srcPath * 源文件路径 * @param destPath * 目标文件路径 * @throws Exception */ public static void decompress(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); decompress(srcFile, destPath); } /** * 文件 解压缩 * * @param destFile * 目标文件 * @param zis * ZipInputStream * @throws Exception */ private static void decompress(File destFile, ZipInputStream zis) throws Exception { ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { // 文件 String dir = destFile.getPath() + File.separator + entry.getName(); File dirFile = new File(dir); // 文件检查 fileProber(dirFile); if (entry.isDirectory()) { dirFile.mkdirs(); } else { decompressFile(dirFile, zis); } zis.closeEntry(); } } /** * 文件探针 * * * 当父目录不存在时,创建目录! * * * @param dirFile */ private static void fileProber(File dirFile) { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { // 递归寻找上级目录 fileProber(parentFile); parentFile.mkdir(); } } /** * 文件解压缩 * * @param destFile * 目标文件 * @param zis * ZipInputStream * @throws Exception */ private static void decompressFile(File destFile, ZipInputStream zis) throws Exception { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); int count; byte data[] = new byte[BUFFER]; while ((count = zis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.close(); } public static void main(String[] args) { // 解压到指定目录 try { zipFiletest2.decompress("D:\\sumZip\\co.zip", "D:\\log"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
参考文章:http://snowolf.iteye.com/blog/642492
时间: 2024-12-29 06:46:10