TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如:
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis); [/code] 需要注意的是,在构建解压文件时,需要考虑目录的自动创建,这里通过递归方式逐层创建父目录,如下所示:
/**
* 文件探针
*
* <pre>
* 当文件目录不存在时,创建目录!
* </pre>
*
* @param dirFile
*/
private static void fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
return;
}
if (!dirFile.exists()) {
dirFile.mkdir();
}
}
[/code] 在压缩的时候,我们是将一个一个文件作为压缩添加项(ZipEntry)添加至压缩包中,解压缩就要将一个一个压缩项从压缩包中提取出来,如下所示:
/**
* 文件 解压缩
*
* @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);
if (entry.isDirectory()){
dirFile.mkdirs();
}else{
decompressFile(dirFile, zis);
}
}
}
[/code] 最核心的解压缩实现,其实与压缩实现非常相似,代码如下所示:
/**
* 文件解压缩
*
* @param destFile
* 目标文件
* @param zis
* ZipInputStream
* @throws Exception
*/
private static void decompressFile(File destFile, ZipInputStream zis)
throws Exception {
fileProber(destFile.getParentFile());
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.write(data);
bos.close();
}
[/code]
来个完整的解压缩实现,代码如下:
/**
* 2010-4-12
*/
package org.zlex.commons.io;
import java.io.BufferedInputStream;
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.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* ZIP压缩工具
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @since 1.0
*/
public class ZipUtils {
public static final String EXT = ".zip";
private static final String BASE_DIR = "";
private static final String PATH = "/";
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 {
fileProber(destFile);
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
decompress(destFile, zis);
zis.closeEntry();
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);
if (entry.isDirectory()){
dirFile.mkdirs();
}else{
decompressFile(dirFile, zis);
}
}
}
/**
* 文件探针
*
* <pre>
* 当文件目录不存在时,创建目录!
* </pre>
*
* @param dirFile
*/
private static void fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
return;
}
if (!dirFile.exists()) {
dirFile.mkdir();
}
}
/**
* 文件解压缩
*
* @param destFile
* 目标文件
* @param zis
* ZipInputStream
* @throws Exception
*/
private static void decompressFile(File destFile, ZipInputStream zis)
throws Exception {
fileProber(destFile.getParentFile());
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.write(data);
bos.close();
}
}
[/code] 其实,理解了ZIP的工作原理,这些代码看起来很好懂!
把刚才做的压缩文件再用上述代码解开看看,测试用例如下:
/**
* 2010-4-12
*/
package org.zlex.commons.io;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public class ZipUtilsTest {
/**
*
*/
@Test
public void test() throws Exception {
// 解压到指定目录
ZipUtils.decompress("d:\f.txt.zip", "d:\ff");
// 解压到当前目录
ZipUtils.decompress("d:\fd.zip");
}
}
[/code] java原生的ZIP实现虽然在压缩时会因与系统字符集不符产生中文乱码,但在解压缩后,字符集即可恢复。
除了java原生的ZIP实现外,commons和ant也提供了相应的ZIP算法实现.
源码下载:http://file.javaxxz.com/2014/10/28/235614828.zip |
|