java解压
来源:互联网 发布:粤语歌曲谐音软件 编辑:程序博客网 时间:2024/06/05 01:54
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.log4j.Logger;
/**
* 压缩包工具类
*/
public class ZipUtil {
private static final Logger logger = Logger.getLogger(ZipUtil.class);
/**
* 解压zip文件
* @param zipFile
* @param unzipFilePath 解压后存放的路径
* @return 返回解压的路径文件夹
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static String unzip(File zipFile, String unzipFilePath) throws Exception{
logger.trace("【update】解压文件:"+zipFile+"到路径:"+unzipFilePath);
String unzipPath = "";
//判断文件是否存在
if(!zipFile.exists() || zipFile.length()<=0){
unzipPath="false";
return unzipPath;
}
if(zipFile.length()<=0){
unzipPath="false";
return unzipPath;
}
//创建解压缩文件保存的路径
File unzipFileDir = new File(unzipFilePath);
//的判断文件夹是否存在如果存在则不创建 如果不存在 则创建
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()){
//创建文件夹
unzipFileDir.mkdirs();
}
//开始解压
logger.trace("开始解压....");
//创建解压对象
ZipEntry zipEntry = null;
//文件保存路径路径
String entryFilePath = null;
//文件夹路径
String entryDirPath = null;
//创建问价对象
File entryFile = null;
//创建文件夹对象
File entryDir = null;
int index = 0, count = 0, bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
//创建输出字符流
BufferedInputStream bufferedInputStream = null;
//创建输入字符流
BufferedOutputStream bufferedOutputStream = null;
try {
//创建压缩文件对象
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();
//第一步循环创建文件夹 第二步创建文件 第三部写入文件
while(entries.hasMoreElements()){
zipEntry = entries.nextElement();
logger.debug("当前的zip对象zipEntry:"+zipEntry.getName());
logger.debug("当前解压路径unzipFilePath:"+unzipFilePath);
boolean isDir = zipEntry.isDirectory();
//当前文件为文件夹
if(isDir){
logger.debug("当前是个文件夹..."+zipEntry.getName());
String dir = zipEntry.getName();
entryFilePath = unzipFilePath + dir + "//";
logger.debug("当前文件夹的完整路径是:"+entryFilePath );
//定义文件夹
entryDir = new File(entryFilePath);
//如果文件夹路径不存在,则创建文件夹
if (!entryDir.exists() || !entryDir.isDirectory()){
entryDir.mkdirs();
logger.debug("创建文件夹:"+entryFilePath);
}
}else{
//当前是个文件
logger.debug("判断当前是个文件:"+zipEntry.getName());
entryFilePath = unzipFilePath + zipEntry.getName();
logger.debug("当前文件的完整路径是entryFilePath:"+entryFilePath);
File f = new File(entryFilePath);
if (index != -1){
entryDirPath = f.getAbsolutePath().split(f.getName())[0];
}else{
entryDirPath = "";
}
logger.debug("entryDirPath:"+entryDirPath);
unzipPath = entryDirPath;
//定义文件夹
entryDir = new File(entryDirPath);
//如果文件夹路径不存在,则创建文件夹
if (!entryDir.exists() || !entryDir.isDirectory()){
entryDir.mkdirs();
}
//创建解压文件
entryFile = new File(entryFilePath);
//写入文件
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(entryFile));
//读取文件
bufferedInputStream = new BufferedInputStream(zip.getInputStream(zipEntry));
//文件写入
while ((count = bufferedInputStream.read(buffer, 0, bufferSize)) != -1){
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
logger.debug("文件解压完毕...重新组装的路径是:"+unzipPath);
}finally{
try {
if(null != bufferedInputStream){
bufferedInputStream.close();
}
if(null != bufferedOutputStream){
bufferedOutputStream.close();
}
} catch (Exception e2) {
}
}
return unzipPath;
}
public static void main(String[] args){
File zipFile = new File("D:\\P01.000243.Z");
System.out.println(zipFile.getName());
try {
// CompressorInputStream //zStreamFactory.getCompressorStream(null);
// zStreamFactory.getCompressorStream(new FileInputStream(new File("D://P01.000243.Z")));
// boolean result = zipFile.renameTo(file);
// System.out.println(result);
// ZipUtil.unzip(file, "C://");
System.out.println("解压完成!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
阅读全文
0 0
- java解压
- java 解压
- java解压
- java解压
- java 解压zip压缩文件
- java解压zip源码
- java 压缩与解压
- Java解压zip文件
- Java 解压文件
- java 压缩、解压
- java解压和压缩文件
- JAVA压缩/解压ZIP
- java解压RAR压缩文件
- java 文件解压
- Java压缩/解压ZIP
- Java解压zip
- java解压zip包
- java解压文件失败
- 产品经理必读的九步法
- 云计算环境下的云存储,安全吗?
- Cannot add PPA: 'ppa:'. Please check that the PPA name or format is correct
- iptables配置NAT
- influxdb基本操作
- java解压
- Sublime Text 快捷键
- 神经网络API、Kotlin支持,那些你必须了解的Android 8.1预览版和Android Studio 3.0新特性
- JVM常见面试题深度总结
- 如何用java线程池做分批次查询处理 java线程池ThreadPoolExecutor的使用
- 链家数据爬取+地图找房
- androidstudio ftp发布apk
- java实现冒泡排序,选择排序,直接插入排序,快速排序
- Webpack经典入门