解压(.rar及.zip格式)压缩文件
2016-06-15 16:59
316 查看
项目需求解压.rar及.zip格式的压缩文件:
package com.sls.framework.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
/**
* <p>
* Title: 解压缩文件
* </p>
* <p>
* Copyright: Copyright (c) 2010
* </p>
* <p>
* Company: yourcompany
* </p>
*
* @author yourcompany
* @version 1.0
*/
public class CompressFile {
/**
* 压缩文件
*
* @param srcfile
* File[] 需要压缩的文件列表
* @param zipfile
* File 压缩后的文件
*/
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipfile));
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
String str = srcfile[i].getName();
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
System.out.println("压缩完成.");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压缩zip包
* @param zipFilePath
* zip文件路径
* @param targetPath
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
* @throws IOException
* @author yayagepei
* @date 2008-9-28
*/
public static void unzip(String zipFilePath, String targetPath)
throws IOException {
OutputStream os = null;
InputStream is = null;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(zipFilePath);
String directoryPath = "";
if (null == targetPath || "".equals(targetPath)) {
directoryPath = zipFilePath.substring(0, zipFilePath
.lastIndexOf("."));
} else {
directoryPath = targetPath;
}
Enumeration entryEnum = zipFile.getEntries();
if (null != entryEnum) {
ZipEntry zipEntry = null;
while (entryEnum.hasMoreElements()) {
zipEntry = (ZipEntry) entryEnum.nextElement();
if (zipEntry.isDirectory()) {
directoryPath = directoryPath + File.separator
+ zipEntry.getName();
System.out.println(directoryPath);
continue;
}
if (zipEntry.getSize() > 0) {
// 文件
File targetFile = buildFile(directoryPath
+ File.separator + zipEntry.getName(), false);
os = new BufferedOutputStream(new FileOutputStream(
targetFile));
is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[4096];
int readLen = 0;
while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
os.write(buffer, 0, readLen);
}
os.flush();
os.close();
} else {
// 空目录
buildFile(directoryPath + File.separator
+ zipEntry.getName(), true);
}
}
}
} catch (IOException ex) {
throw ex;
} finally {
if(null != zipFile){
zipFile = null;
}
if (null != is) {
is.close();
}
if (null != os) {
os.close();
}
}
}
public static File buildFile(String fileName, boolean isDirectory) {
File target = new File(fileName);
if (isDirectory) {
target.mkdirs();
} else {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
target = new File(target.getAbsolutePath());
}
}
return target;
}
/**
* 根据原始rar路径,解压到指定文件夹下.
* @param srcRarPath 原始rar路径
* @param dstDirectoryPath 解压到的文件夹
*/
public static void unRarFile(String srcRarPath,String dstDirectoryPath) {
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
Archive a = null;
try {
a = new Archive(new File(srcRarPath));
if (a != null) {
a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
if (fh.isDirectory()) { // 文件夹
File fol = new File(dstDirectoryPath + File.separator
+ fh.getFileNameString());
fol.mkdirs();
} else { // 文件
File out = new File(dstDirectoryPath + File.separator
+ fh.getFileNameString().trim());
//System.out.println(out.getAbsolutePath());
try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.sls.framework.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
/**
* <p>
* Title: 解压缩文件
* </p>
* <p>
* Copyright: Copyright (c) 2010
* </p>
* <p>
* Company: yourcompany
* </p>
*
* @author yourcompany
* @version 1.0
*/
public class CompressFile {
/**
* 压缩文件
*
* @param srcfile
* File[] 需要压缩的文件列表
* @param zipfile
* File 压缩后的文件
*/
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipfile));
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
String str = srcfile[i].getName();
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
System.out.println("压缩完成.");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压缩zip包
* @param zipFilePath
* zip文件路径
* @param targetPath
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
* @throws IOException
* @author yayagepei
* @date 2008-9-28
*/
public static void unzip(String zipFilePath, String targetPath)
throws IOException {
OutputStream os = null;
InputStream is = null;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(zipFilePath);
String directoryPath = "";
if (null == targetPath || "".equals(targetPath)) {
directoryPath = zipFilePath.substring(0, zipFilePath
.lastIndexOf("."));
} else {
directoryPath = targetPath;
}
Enumeration entryEnum = zipFile.getEntries();
if (null != entryEnum) {
ZipEntry zipEntry = null;
while (entryEnum.hasMoreElements()) {
zipEntry = (ZipEntry) entryEnum.nextElement();
if (zipEntry.isDirectory()) {
directoryPath = directoryPath + File.separator
+ zipEntry.getName();
System.out.println(directoryPath);
continue;
}
if (zipEntry.getSize() > 0) {
// 文件
File targetFile = buildFile(directoryPath
+ File.separator + zipEntry.getName(), false);
os = new BufferedOutputStream(new FileOutputStream(
targetFile));
is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[4096];
int readLen = 0;
while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
os.write(buffer, 0, readLen);
}
os.flush();
os.close();
} else {
// 空目录
buildFile(directoryPath + File.separator
+ zipEntry.getName(), true);
}
}
}
} catch (IOException ex) {
throw ex;
} finally {
if(null != zipFile){
zipFile = null;
}
if (null != is) {
is.close();
}
if (null != os) {
os.close();
}
}
}
public static File buildFile(String fileName, boolean isDirectory) {
File target = new File(fileName);
if (isDirectory) {
target.mkdirs();
} else {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
target = new File(target.getAbsolutePath());
}
}
return target;
}
/**
* 根据原始rar路径,解压到指定文件夹下.
* @param srcRarPath 原始rar路径
* @param dstDirectoryPath 解压到的文件夹
*/
public static void unRarFile(String srcRarPath,String dstDirectoryPath) {
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
Archive a = null;
try {
a = new Archive(new File(srcRarPath));
if (a != null) {
a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
if (fh.isDirectory()) { // 文件夹
File fol = new File(dstDirectoryPath + File.separator
+ fh.getFileNameString());
fol.mkdirs();
} else { // 文件
File out = new File(dstDirectoryPath + File.separator
+ fh.getFileNameString().trim());
//System.out.println(out.getAbsolutePath());
try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 记录Mac下安装pyenv时所遇到的问题
- iOS 使用cocoapods 找不到头文件
- 根据展示文字自适应 cell 高度,实现点击cell的伸缩扩展
- 使用MyBatis链接MySQL
- linux多线程服务器编程 muduo库学习笔记
- Android 通过USB查看kernel调试信息
- html手机端 解决滑动跟点击的冲突问题
- SAP STO小解
- 面向接口编程之二——编程实例
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(20)----模型,网格和材质
- python学习笔记 操作文件和目录
- HTML特殊字符编码对照表
- Android初级教程短信防火墙
- 五.字符串和编码
- Android初级教程短信防火墙
- 移动端网络优化
- UILabel设置顶、左、右、底部对齐
- 修改数组大小扩充数组
- SSL协议详解
- HelloWorld帧速率