设为首页 加入收藏

TOP

Java使用基本JDK操作ZIP文件(二)
2014-11-23 17:50:37 来源: 作者: 【 】 浏览:79
Tags:Java 使用 基本 JDK 操作 ZIP 文件
File);
try {
IOUtils.copy(is, zipOut);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
return;
}
for (File file : srcFile.listFiles()) {
String entryName = ns + file.getName();


if (file.isDirectory()) {
entryName += File.separator;
zipOut.putNextEntry(new ZipEntry(entryName));
}
doZipFile(file, zipOut, entryName);
}
}

/**
* 将指定的压缩文件解压到指定的目标目录下.
* 如果指定的目标目录不存在或其父路径不存在, 将会自动创建.
*
* @param zipFile 将会解压的压缩文件
* @param destFile 解压操作的目录
*/
public static void unzipFile(String zipFile,String destFile) throws IOException {
unzipFile(new File(zipFile), new File(destFile));
}

/**
* 将指定的压缩文件解压到指定的目标目录下.
* 如果指定的目标目录不存在或其父路径不存在, 将会自动创建.
*
* @param zipFile 将会解压的压缩文件
* @param destFile 解压操作的目录目录
*/
public static void unzipFile(File zipFile,File destFile) throws IOException {
unzipFile(FileUtils.openInputStream(zipFile), destFile);
// doUnzipFile(new ZipFile(zipFile), destFile);
}

public static void doUnzipFile(ZipFile zipFile,File dest) throws IOException {
if (!dest.exists()) {
FileUtils.forceMkdir(dest);
}
Enumeration entries = (Enumeration) zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File file = new File(dest, entry.getName());
if (entry.getName().endsWith(File.separator)) {
FileUtils.forceMkdir(file);
} else {
OutputStream out = FileUtils.openOutputStream(file);
InputStream in = zipFile.getInputStream(entry);
try {
IOUtils.copy(in, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(out);
}
}
}
zipFile.close();
}

/**
* 将指定的输入流解压到指定的目标目录下.
*
* @param is 将要解压的输入流
* @param destFile 解压操作的目标目录
* @throws IOException
*/
public static void unzipFile(InputStream is,File destFile) throws IOException {
try {
if (is instanceof ZipInputStream) {
doUnzipFile((ZipInputStream) is,destFile);
} else {
doUnzipFile(new ZipInputStream(is), destFile);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
}

/**
*
* @param zipInput
* @param dest
* @throws IOException
*/
private static void doUnzipFile(ZipInputStream zipInput, File dest)
throws IOException {
if (!dest.exists()) {
FileUtils.forceMkdir(dest);
}
for (ZipEntry entry; (entry = zipInput.getNextEntry()) != null;) {
String entryName = entry.getName();


File file = new File(dest, entry.getName());
if (entryName.endsWith(File.separator)) {
FileUtils.forceMkdir(file);
} else {
OutputStream out = FileUtils.openOutputStream(file);
try {
IOUtils.copy(zipInput, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(out);
}
}
zipInput.closeEntry();
}
}

// 测试
public static void main(String[] args) {
try {
// zipFile("M:\\ZIP\\test\\tempZIP", "M:\\ZIP\\test\\bbc.zip");
unzipFile("M:\\ZIP\\test\\bbc.zip", "M:\\ZIP\\test\\bb\\");
} catch (IOException e) {
e.printStackTrace();
}
}
}


注:
1、详细情况请参照JDK文档


2、JDK自身的ZIP包不支持中文,所以网上有很多介绍Apache下的解决方案,此处不作描述,文章太长了就更加臭了,另起一篇写。


3、不支持密码,网上另有支持密码的项目,如果可能,另起文章介绍吧!


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java中的函数传递 下一篇Java解压带密码的RAR压缩文件

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: