java解压zip文件的代码
2011-12-01 20:47
417 查看
[b]java解压zip文件的代码[/b]
/**
* 解压zip格式的压缩包
*
* @param filePath
* 压缩文件路径
* @param outPath
* 输出路径
* @return 解压成功或失败标志
*/
public static Boolean unZip(String filePath, String outPath) {
String unzipfile = inPath; // 解压缩的文件名
try {
ZipInputStream zin = new ZipInputStream(new FileInputStream(
unzipfile));
ZipEntry entry;
// 创建文件夹
while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(outPath, entry.getName());
if (!directory.exists()) {
if (!directory.mkdirs()) {
System.exit(0);
}
}
zin.closeEntry();
} else {
File myFile = new File(entry.getName());
FileOutputStream fout = new FileOutputStream(outPath
+ myFile.getPath());
DataOutputStream dout = new DataOutputStream(fout);
byte[] b = new byte[1024];
int len = 0;
while ((len = zin.read(b)) != -1) {
dout.write(b, 0, len);
}
dout.close();
fout.close();
zin.closeEntry();
}
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 解压zip格式的压缩包
*
* @param filePath
* 压缩文件路径
* @param outPath
* 输出路径
* @return 解压成功或失败标志
*/
public static Boolean unZip(String filePath, String outPath) {
String unzipfile = inPath; // 解压缩的文件名
try {
ZipInputStream zin = new ZipInputStream(new FileInputStream(
unzipfile));
ZipEntry entry;
// 创建文件夹
while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(outPath, entry.getName());
if (!directory.exists()) {
if (!directory.mkdirs()) {
System.exit(0);
}
}
zin.closeEntry();
} else {
File myFile = new File(entry.getName());
FileOutputStream fout = new FileOutputStream(outPath
+ myFile.getPath());
DataOutputStream dout = new DataOutputStream(fout);
byte[] b = new byte[1024];
int len = 0;
while ((len = zin.read(b)) != -1) {
dout.write(b, 0, len);
}
dout.close();
fout.close();
zin.closeEntry();
}
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
相关文章推荐
- Java解压zip文件的关键代码
- Java解压zip文件完整代码分享
- 在Java代码中使用ZipFile解压zip文件
- Java压缩/解压文件的实现代码
- Java无需解压直接读取Zip文件和文件内容
- 通过java解决linux下解压来自window生成的zip文件的乱码问题
- java应用压缩、解压zip文件
- 代码写解压zip文件
- Java压缩、解压zip文件(依赖第三方类库zip4j_1.3.1.jar)
- java I/O:解压ZIP文件并保存其目录结构
- java--解压zip文件
- java解压zip,rar的代码
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
- [Java代码]Java中压缩与解压--中文文件名乱码解决办法
- java对zip文件的解压处理直接访问压缩文件
- Java自动解压文件实例代码
- 使用java解压或者压缩zip文件
- 通过Java生成加密压缩文件(支持AES压缩和解压zip文件)
- AS3 和 Java 密码解压zip文件
- java解压zip文件