java文件解压文件_java 文件解压缩
直接上代码:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/*
* 对特定文件进行解压缩
*/
public class ZipAndUnZipUtil {
private ZipAndUnZipUtil() {
}
private static ZipAndUnZipUtil zipSingleInstance;
/*
* 生成实例
*/
public static ZipAndUnZipUtil getInstance() {
if (null == zipSingleInstance) {
zipSingleInstance= new ZipAndUnZipUtil();
}
return zipSingleInstance;
}
/*
* 解压文件
* @param srcPath 源压缩包路径
* @param despath 解压后存储的目录
* @return 解压后存储的路径目录
*/
@SuppressWarnings("unchecked")
public String unZip(String srcPath, String desPath) {
if ("".equals(srcPath) || null == srcPath) {
System.out.println("解压的文件的路径为空");
return null;
}
if ("".equals(srcPath) || null == desPath) {
System.out.println("文件解压后存储路径为");
return null;
}
File srcFile = new File(srcPath);
File desFile = new File(desPath);
//创建指定目录,如果其不存在的情况下
if (!desFile.exists()) {
if (!desFile.mkdirs()) {
System.out.println("指定目录创建失败");
return null;
}
}
try {
//读取zip文件
ZipFile zipTest = new ZipFile(srcFile);
Enumeration entries = (Enumeration) zipTest
.entries();
ZipEntry entry = null;
while (entries.hasMoreElements()) {
entry = entries.nextElement();
if (entry.isDirectory()) {
//创建目录
File dirFile=new File(desPath+File.separator+entry.getName());
dirFile.mkdirs();
} else {
//解压文件
BufferedInputStream inputStream = new BufferedInputStream(zipTest.getInputStream(entry));
BufferedOutputStream out =new BufferedOutputStream( new FileOutputStream(desPath+File.separator+entry.getName()));
byte[] bytes=new byte[2*1024];
int count;
while((count=inputStream.read(bytes))!=-1){
out.write(bytes, 0, count);
}
inputStream.close();
out.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return desPath;
}
/*
* 压缩文件
*
* @param srcPath 需要压缩的文件路径
* @param despath 压缩文件的存储目录
* @param name 文件名字
* @return desPath 压缩文件结果的存储路径,如果输入路径为空,或者存储路径已经存在,则返回null,
*/
public String zipDocuments(String srcPath, String desPath,String name) {
if ("".equals(srcPath) || null == srcPath) {
System.out.println("压缩的文件的路径为空");
return null;
}
if ("".equals(srcPath) || null == desPath) {
System.out.println("压缩文件的存储路径为");
return null;
}
File srcFile = new File(srcPath);
File desFile = new File(desPath);
// 判断目标目录是否存在,如果不存在就创建
if (!desFile.exists()) {
if (!desFile.mkdirs()) {
System.out.println("存储目录创建失败");
return null;
}
}
try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(
desPath + File.separator + name));
int start = srcPath.lastIndexOf(srcFile.getName());
// 压缩文件
zipDirAndFile(srcFile, outZip, start);
outZip.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return desPath + File.separator + name;
}
/*
* 压缩文件及目录
*/
private void zipDirAndFile(File file, ZipOutputStream out, int start) {
if (null == file | null == out) {
throw new NullPointerException("文件或ZipOutputStream引用为空");
}
File[] files;
// 如果file对象是目录
if (file.isDirectory()) {
files = file.listFiles();
try {
// 生成压缩目录文件
String dirPath = file.getPath().substring(start);
out.putNextEntry(new ZipEntry(absToreDirPath(dirPath)));
out.closeEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (File f : files) {
zipDirAndFile(f, out, start);
}
} else {
// 如果file对象时文件
BufferedInputStream inZip;
byte[] buff;
try {
inZip = new BufferedInputStream(new FileInputStream(file));
buff = new byte[5 * 1024];
ZipEntry zipEntry = new ZipEntry(new String(file.getPath()
.substring(start)));
out.putNextEntry(zipEntry);
int count;
while ((count = inZip.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, count);
}
inZip.close();
out.closeEntry();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 路径转换成相对路径 start:相对路径起始的位置
*/
private String absToreDirPath(String absPath) {
char[] pathChars = absPath.toCharArray();
for (int i = 0; i < pathChars.length; i++) {
if (File.separatorChar == pathChars[i]) {
pathChars[i] = '/';
}
}
return new String(pathChars) + "/";
}
}
java文件解压文件_java 文件解压缩相关推荐
- Linux将文件解压到指定文件夹*
Linux将文件解压到指定文件夹 tar -zxvf xxx.tar.gz -C /xxx/ tar 命令不管是解压还是压缩都会用到,是一个非常常用的命令 上述命令具体的参数详解如下 -z是使用gzi ...
- java zip 解压 密码_Java解压和压缩带密码的zip文件过程详解
前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...
- java中解压tar.gz文件
在开发中我们经常需要对gz文件进行解压缩,在java中解压gz文件还是比较繁琐的,为此写了一个工具类方便需要的时候可以直接拿过来用.代码如下: package com.eggsl.utils;impo ...
- Java代码解压RAR/ZIP文件
pom.xml <!-- 导入zip解压包 --> <dependency><groupId>ant</groupId><artifactId&g ...
- bundle文件解压_通过sourcemap解压缩webpack 实战
现在许多网站都使用webpack对网站打包,许多前端框架也默认配置好webpack.这会在渗透测试或挖洞过程中带来一些麻烦.这让我们极其痛苦.但是开发者忽视起潜在风险,在线上环境使用了开发环境的配置, ...
- java程序解压/压缩.gz文件
压缩成.gz格式 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOu ...
- 关于taz文件解压成tar文件这件事(根源在于7-Zip)
最近在看图像分类的代码,从劈里啪啦那里得到一份花分类的数据集 具体的实现过程参照pytorch--AlexNet--训练花分类数据集_heart_6662的博客-CSDN博客_花分类数据集 链接实现过 ...
- java zip解压 中文_java解压ZIP 解决中文乱码 (GBK和UTF-8)
java解压ZIP 解决中文乱码 (GBK和UTF-8) 工具使用 : zip4j GitHub : zip4j 版本 : 2.2.8 Maven : net.lingala.zip4j zip4j ...
- java zipfile用法_Java使用ZipFile类实现Zip文件解压
java.util.zip.ZipFile类用于从 ZIP 文件读取条目. 即从给定的ZIP压缩文件中获取所有文件的信息,如:文件的名称.是否为目录等信息.可以使用这个类来实现将zip文件进行解压操作 ...
- java util zip.zipexc,JAVA解压zip压缩文件的实例
今天在弄一个东西,需要在PL/SQL中解压zip的压缩包,刚开始的时候是想着直接在PLSQL中调用java,在java里面调用unzip的shell命令来解析压缩文件,但是比较悲剧,一直老是失败,在尝 ...
最新文章
- Ubuntu 系统禁止或者改变中文简体切换繁体,方便使用AS全局搜索
- mac mini 装UBUNTU后没有WIFI解决办法
- java txtreader_一个简单的Java读写文件例子
- 病的不轻?教你 2 招,拯救拖延症!
- C#之获取网页标题...
- .netframework3.5 中TimeZoneInfo 类的使用
- ifix虚拟服务器,ifix的客户端和服务器
- awk 内嵌正则 提取字符串_使用awk提取字符串中的数字或字母
- oracle的服務監聽器,Oracle监听器和服务名的配置
- 大数据数据科学家常用面试题_面试有关数据科学,数据理解和准备的问答
- POJ1703-Find them, Catch them
- Java序列化中的SerialVersionUid
- 通过刷bios的方式在win8.1平板上启动windows phone模拟器
- ios开发闹钟步骤_苹果快捷指令自动化(起床关闹钟后自动播放音乐)
- 2008下mysql补丁_windows Server 2008 R2安装Mysql 8的打补丁顺序
- 数据仓库工具hive面试题集锦
- Android studio底部Logcat模块不见了以及Locat日志中包含了很多无用的错误日志筛选方法
- HTML5多媒体(音频、视频播放)
- java导出帆软pdf,java后台把fineRepo图表导出pdf格式时发生错误!
- VOIP技术发展综述与外呼系统
热门文章
- 解析URI与URL之间的区别与联系
- centos arm-linux-gcc,CentOS 6.4配置arm-linux-gcc交叉环境
- hadoop伪分布式搭建 java_hadoop2.2.0伪分布式搭建
- javaScript入门基础说明
- 计算机控制论文,计算机控制系统论文.ppt
- gateway中的局部过滤器_Spring Cloud Gateway中的过滤器工厂:重试过滤器
- jQuery表单验证的几种方法
- 利用canvas来绘制一个会动的图画
- 激发你的灵感:16个精美视差效果网页设计作品
- angularjs的$http请求方式