<!-- 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文件的更多相关文章

  1. java解压多目录Zip文件(解决中文乱码问题)--转载

    原文地址:http://zhangyongbo.iteye.com/blog/1749439 import java.io.BufferedOutputStream; import java.io.F ...

  2. C#压缩或解压(rar和zip文件)

    /// <summary> /// 解压RAR和ZIP文件(需存在Winrar.exe(只要自己电脑上可以解压或压缩文件就存在Winrar.exe)) /// </summary&g ...

  3. Java解压上传zip或rar文件,并解压遍历文件中的html的路径

    1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...

  4. python-----自动解压并删除zip文件

    如何自动解压并删除zip? 如何解压  →  使用内置模块来实现(shutil.unpack_archive) 如何删除zip  →  使用内置模块os来实现(os.remove) 如何监测zip的出 ...

  5. 在Ubuntu系统中解压rar和zip文件的方法

    大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...

  6. Linux中解压、压缩 ZIP文件

    解压 unzip -o -d /home/v-gazh myfile.zip # 把myfile.zip文件解压到 /home/v-gazh/ # -o:不提示的情况下覆盖文件: # -d:-d /h ...

  7. JAVA 解压压缩包中指定文件或实现压缩文件的预览及下载单个或多个指定的文件

    业务逻辑中还要判读用户是否有此文件的防问权限 2017-04-20 新增文件与文件夹图标显示及过滤高亮显示功能: 2017-05-20 新增搜索向前及向后.及更新下载功能.更新文件路径显示: 测试地址 ...

  8. 【转】JAVA解压.TAR.Z及.ZIP文件

     解压.ZIP文件 package app.qdupr.Method; import java.io.File; import java.io.FileOutputStream; import jav ...

  9. Java 压缩/ 解压 .Z 文件

    1.问题描述 公司项目有需要用 JAVA 解压 .z文件. .z 是 unix 系统常见的压缩文件. 2.源码 import com.chilkatsoft.CkUnixCompress; impor ...

随机推荐

  1. kali 安装 docker

    添加密钥 信任 浙大更新源 curl -fsSL http://mirrors.zju.edu.cn/docker-ce/linux/debian/gpg | sudo apt-key add - t ...

  2. WPF行为基础

    理解行为 复杂的UI效果(缩放.拖拽.平滑等)通过样式与触发器比较难以实现,通过引入行为模型来实现.使用行为也可以处理UI操作之外的业务 程序集引用 System.Windows.Interactiv ...

  3. 交换机做节点的vlan划分

    基础准备 准备一台个人pc,两台物理服务器和一台三层交换机,以及网线若干. ip地址规划如下: 主机名 IP 控制节点 192.168.100.0 计算节点 192.168.200.0 个人pc 19 ...

  4. 解决:Could not resolve dependencies for project xxx: Could not find artifact xxx

    引言 运行A module,找不到B module的依赖报错.A.B module都在project中. 报错信息 [INFO] Scanning for projects... [INFO] [IN ...

  5. CNN-Backbone的Pytorch实现

    创建日期: 2020-07-04 17:19:39 简介:卷积神经网络非常适合处理图像相关任务,其优势一是权值共享策略,降低了模型复杂度和参数量,本质上也对应着生物视觉神经的感受野.二是其强大的特征提 ...

  6. Nacos源码系列—服务端那些事儿

    点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你,很高兴能够成为你的朋友. 项目源码地址:公众号回复 nacos,即可免费获取源码 前言 在上节课中,我们讲解了客户端注册服 ...

  7. RestFul和控制器

    RestFul和控制器 控制器Controller 控制器复杂提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现. 控制器负责解析用户的请求并将其转换为一个模型. 在Spring MVC中 ...

  8. vmware 安装的虚拟机没有网络

    前提:需要先将 vmware 软件里的所有虚拟机关机 查看以下两个服务是否启动 如果以上两个服务未启动,就全部启动起来,如果某一个在启动时报错,就打开 vmware 软件,执行以下操作 编辑 > ...

  9. Jenkins安装详解

    一.Jenkins是什么 Jenkins是一个独立的开源自动化服务器,可用于自动执行与构建,测试,交付或者部署软件相关的各种任务,是跨平台持续集成和持续交付应用程序,提高工作效率.使用Jenkins不 ...

  10. 渗透:dSploit

    dSploit--开源的专业的Android平台安全管理工具包 只能在横屏模式下工作,即使你旋转你的设备也将继续保持横屏,如果你有一个应用程序,如旋转控制器,迫使每一个应用程序旋转,将导致dSploi ...