JAVA解压.Z及.ZIP文件
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
package app.qdupr.Method;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.z.ZCompressorInputStream;
/**
* 自动解压.Z格式的文件
* @author Administrator
*
*/ public class ZCompress { /**
* 测试方法
* @param args
*/
public static void main(String[] args) {
ZCompress zip = new ZCompress();
File dir = new File("E:\\Test");
File[] subs = dir.listFiles();
for (File sub : subs) { String file=sub.getParent()+File.separator+sub.getName();
zip.uncompress(file);
System.out.println(file);
}
} /**
* 获取文件夹下全部文件
* @param file
*/
public static void findAllfile(String file) {
ZCompress zip = new ZCompress();
File dir = new File(file);
File[] subs = dir.listFiles();
for (File sub : subs) {
String newfile=sub.getParent()+File.separator+sub.getName();
zip.uncompress(newfile);
}
}
/**
* .Z文件解压
* @param file
*/
public void uncompress(String file) {
//File file =new File("E:\\BEA20151101.Z");
ZCompress.deCompressTZFile(file);
}
/**
* 将String型转换为File类型
* @param file
* @return
*/
public static File deCompressTZFile(String file) {
return deCompressZFile(new File(file));
}
/**
* 执行解压
* @param file
* @return
*/
private static File deCompressZFile(File file) {
int buffersize = 2048;
FileOutputStream out = null;
ZCompressorInputStream zIn = null;
try {
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
//解压后的文件存放路径及文件名
String name = file.getName().substring(0, file.getName().indexOf("."));
File outFile = new File("E:\\File\\"+name);
out = new FileOutputStream(outFile);
zIn = new ZCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = zIn.read(buffer))) {
out.write(buffer, 0, n);
}
return outFile;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
out.close();
zIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* .TAR格式文件解压
* @param file
*/
private static void deCompressTARFile(File file) {
int buffersize = 2048;
String basePath = file.getParent() + File.separator;
TarArchiveInputStream is = null;
try {
is = new TarArchiveInputStream(new FileInputStream(file));
while (true) {
TarArchiveEntry entry = is.getNextTarEntry();
if (entry == null) {
break;
}
if (entry.isDirectory()) {// 这里貌似不会运行到,跟ZipEntry有点不一样
new File(basePath + entry.getName()).mkdirs();
} else {
FileOutputStream os = null;
try {
File f = new File(basePath + entry.getName());
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
if (!f.exists()) {
f.createNewFile();
}
os = new FileOutputStream(f);
byte[] bs = new byte[buffersize];
int len = -1;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
os.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
} } }
来源:
JAVA解压.TAR.Z及.ZIP文件
JAVA解压.Z及.ZIP文件的更多相关文章
- java解压多目录Zip文件(解决中文乱码问题)--转载
原文地址:http://zhangyongbo.iteye.com/blog/1749439 import java.io.BufferedOutputStream; import java.io.F ...
- C#压缩或解压(rar和zip文件)
/// <summary> /// 解压RAR和ZIP文件(需存在Winrar.exe(只要自己电脑上可以解压或压缩文件就存在Winrar.exe)) /// </summary&g ...
- Java解压上传zip或rar文件,并解压遍历文件中的html的路径
1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...
- python-----自动解压并删除zip文件
如何自动解压并删除zip? 如何解压 → 使用内置模块来实现(shutil.unpack_archive) 如何删除zip → 使用内置模块os来实现(os.remove) 如何监测zip的出 ...
- 在Ubuntu系统中解压rar和zip文件的方法
大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...
- Linux中解压、压缩 ZIP文件
解压 unzip -o -d /home/v-gazh myfile.zip # 把myfile.zip文件解压到 /home/v-gazh/ # -o:不提示的情况下覆盖文件: # -d:-d /h ...
- JAVA 解压压缩包中指定文件或实现压缩文件的预览及下载单个或多个指定的文件
业务逻辑中还要判读用户是否有此文件的防问权限 2017-04-20 新增文件与文件夹图标显示及过滤高亮显示功能: 2017-05-20 新增搜索向前及向后.及更新下载功能.更新文件路径显示: 测试地址 ...
- 【转】JAVA解压.TAR.Z及.ZIP文件
解压.ZIP文件 package app.qdupr.Method; import java.io.File; import java.io.FileOutputStream; import jav ...
- Java 压缩/ 解压 .Z 文件
1.问题描述 公司项目有需要用 JAVA 解压 .z文件. .z 是 unix 系统常见的压缩文件. 2.源码 import com.chilkatsoft.CkUnixCompress; impor ...
随机推荐
- 面试必问的8个CSS响应式单位,你知道几个?
大家好,我是半夏,一个刚刚开始写文的沙雕程序员.如果喜欢我的文章,可以关注 点赞 加我微信:frontendpicker,一起学习交流前端,成为更优秀的工程师-关注公众号:搞前端的半夏,了解更多前端知 ...
- 最新 x86_64 系统调用入口分析 (基于 5.7.0)
最新 x86_64 系统调用入口分析 (基于5.7.0) 整体概览 最近的工作涉及系统调用入口,但网上的一些分析都比较老了,这里把自己的分析过程记录一下,仅供参考. x86_64位系统调用使用 SYS ...
- iptables系列教程(一)| iptables入门篇
一个执着于技术的公众号 前言 在早期的 Linux 系统中,默认使用的是 iptables 配置防火墙.尽管新型 的 firewalld 防火墙已经被投入使用多年,但是大量的企业在生产环境中依然出于各 ...
- ansible的roles使用
1.创建roles文件夹 mkdir roles 2.在roles文件夹里面创建文件夹 cd roles/ mkdir {nginx,uwsgi,redis,mysql} 3.cd nginx 4.m ...
- python牛顿法求一元多次函数极值
现在用牛顿法来实现一元函数求极值问题 首先给出这样一个问题,如果有这么一个函数$f(x) = x^6+x$,那么如何求这个函数的极值点 先在jupyter上简单画个图形 %matplotlib inl ...
- TS 自学笔记(二)装饰器
TS 自学笔记(二)装饰器 本文写于 2020 年 9 月 15 日 上一篇 TS 文章已经是很久之前了.这次来讲一下 TS 的装饰器. 对于前端而言,装饰器是一个陌生的概念,但是对于 Java.C# ...
- mongodb 复杂查询
记录一下工作中用到的 mongodb 复杂查询 aggregate 筛选 === 等于 { $match: { name: "bob" } } !== 不等于 { $match: ...
- Spring Boot下Spring Batch入门实例
一.About Spring Batch是什么能干什么,网上一搜就有,但是就是没有入门实例,能找到的例子也都是2.0的,看文档都是英文无从下手~~~,使用当前最新的版本整合网络上找到的例子. 关于基础 ...
- unity---GL实现案例
GL C#实现 不管是画任何东西都需要Begin()和End()函数: 画线 using System.Collections; using System.Collections.Generic; u ...
- unity---对象池
当内存占满了,GC会自动清理内存 优化方法:对象池 Invoke("Push",1);//延迟一秒时间调用 高级缓存池 小池子 大池子