一、简述
解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如:
CheckedInputStream cis = new CheckedInputStream(newFileInputStream(
srcFile),newCRC32());
ZipInputStream zis= new ZipInputStream(cis);
需要注意的是,在构建解压文件时,需要考虑目录的自动创建,这里通过递归方式逐层创建父目录,如下所示:
//当父目录不存在时,创建目录!
private static voidfileProber(File dirFile) {
File parentFileParentFile();if (!ists()) {//递归寻找上级目录
fileProber(parentFile);
parentFile.mkdir();
}
}
在压缩的时候,我们是将一个一个文件作为压缩添加项(ZipEntry)添加至压缩包中,解压缩就要将一个一个压缩项从压缩包中提取出来,如下所示:
private static voiddecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry entry= null;while ((entry = NextEntry()) != null) {//文件
String dir = Path() + File.separator Name();
File dirFile= newFile(dir);//文件检查
fileProber(dirFile);if(entry.isDirectory()){
dirFile.mkdirs();
}else{
decompressFile(dirFile, zis);
}
zis.closeEntry();
}
}
最核心的解压缩实现,其实与压缩实现非常相似,代码如下所示:
/**
* 文件解压缩
*
* @param destFile
* 目标文件
* @param zis
* ZipInputStream
* @throws Exception*/
private static voiddecompressFile(File destFile, ZipInputStream zis)
throws Exception {
BufferedOutputStream bos= newBufferedOutputStream(newFileOutputStream(destFile));intcount;byte data[] = new byte[BUFFER];while ((count = ad(data, 0, BUFFER)) != -1) {
bos.write(data,0, count);
}
bos.close();
}
完整的例子:
package st;
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 = Parent();
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 = NextEntry()) != null) {
// 文件
String dir = Path() + File.separator + Name();
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 = ParentFile();
if (!ists()) {
// 递归寻找上级目录
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 = ad(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();
}
}
}
参考文章:
本文发布于:2024-01-28 19:48:18,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064425039845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |