解压.ZIP文件

package app.qdupr.Method;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile; /**
* ZIP文件解压
* @author Administrator
*
*/
public class ZipUncompress { private static int bufSize = 8096; //size of byte public static void main(String[] args) throws Exception
{ String tmpFile = "C:\\Users\\Administrator\\Desktop\\20151101.rar";
ZipUncompress.unZip(tmpFile, tmpFile.substring(0, tmpFile.lastIndexOf(".")));
// -----------------
System.out.println("Usage:");
System.out.println("解压:java ZipFileUtils -unzip fileName.zip");
System.out.println();
}
public static String unZip(String unZipFileName, String destFileName)
{
File unzipFile = new File(unZipFileName); if(destFileName == null || destFileName.trim().length() == 0)
{
destFileName = unzipFile.getParent();
} File destFile;
ZipFile zipFile = null;
try
{
zipFile = new ZipFile(unzipFile, "GBK");
for(Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();)
{
ZipEntry entry = (ZipEntry) entries.nextElement();
destFile = new File(destFileName, entry.getName()); unZipFile(destFile, zipFile, entry); //执行解压
}
} catch(Exception e)
{
return e.getMessage();
}
finally
{
try
{
assert zipFile != null;
zipFile.close();
} catch(Exception e)
{
e.printStackTrace();
}
}
return null;
}
private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry)
throws IOException
{
InputStream inputStream;
FileOutputStream fileOut;
if(entry.isDirectory()) //是目录,则创建之
{
destFile.mkdirs();
}
else //是文件
{
//如果指定文件的父目录不存在,则创建之.
File parent = destFile.getParentFile();
if(parent != null && !parent.exists())
{
parent.mkdirs();
} inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(destFile);
byte[] buf = new byte[bufSize];
int readedBytes;
while((readedBytes = inputStream.read(buf)) > 0)
{
fileOut.write(buf, 0, readedBytes);
}
fileOut.close(); inputStream.close();
}
}
}
解压.TAR.Z文件
所需jar包 commons-compress-1.7及以上

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

  1. Java解压tar.Z文件(使用Apache Commons-compress)

    这里使用apache commons compress对.tar.Z格式文件进行解压. 对于一个文件test.tar.Z,我们可以将解压过程理解为: 将test.tar.Z解压为test.tar: 将 ...

  2. Linux 下面解压.tar.gz 和.gz文件解压的方式

    Linux 下面解压.tar.gz 和.gz文件解压的方式 两种解压方式 1 .tar.gz 使用tar命令进行解压 tar -zxvf java.tar.gz 解压到指定的文件夹 tar -zxvf ...

  3. 解压上传的zip文件流和文件

    /** * 解压上传的zip文件流 * @param stream * @param outputDirectory */ public static String unzip(InputStream ...

  4. Linux压缩解压 tar.gz格式的文件.查看tomcat是否运行

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  5. tar 只解压tar包中某个文件

    sh-4.1# ls test.tar sh-4.1# tar -tf test.tar ./ecs20161207.png ./ecs.png ./ecs.xml ./rds.png ./Scree ...

  6. 【java】 java 解压tar.gz读取内容

    package com.xwolf.stat.util; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson. ...

  7. Linux使用shell解压tar.Z格式文件

    建设当前目录下有一个名为test.tar.Z的文件. 使用如下指令可以将其解压,并将解压后的所有文件放置在当前目录下: zcat test.tar.Z | tar -xvf - 如果想要将解压缩的文件 ...

  8. 【Linux/Ubuntu学习3】解决ubuntu解压windows生成的zip文件时乱码问题

    在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 虽然2005年就有人把这报告为bu ...

  9. 解决ubuntu解压windows生成的zip文件时乱码问题

    在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 虽然2005年就有人把这报告为bu ...

随机推荐

  1. 利用openxml在Excel中插入图表

    using System.Collections.Generic; using System.Linq; using DOD = DocumentFormat.OpenXml.Drawing; usi ...

  2. ie的用户名密码输入框右侧提示去掉

    ::-ms-clear,::-ms-reveal{display:none;} //用户名 密码

  3. php中测试运行的时间,从而选择得出优化程序

    对于新手来说,优化代码的习惯十分重要, 测试运行的时间,从而得出最好的一个 <?php $t1=microtime(true);   //获取程序1,开始的时间 程序1(代码...) $t2=m ...

  4. Python学习过程中各个难点---数据类型篇

    ---恢复内容开始--- 当时在学习python的基本数据类型时,对于可变与不可变类型不是了解的很透彻,这篇是回过头来自己的一些理解. 可变的数据类型有列表,不可变的数据类型有字符串,数字和元组   ...

  5. 黑群晖DS3617xs-DSM6.1.7up3/up2 开启ROOT用户,同时SATA改eSATA,挂载NTFS硬盘设置(二)

    这两天闲来没事在某宝上搞了个黑群晖主机就j1900/4G小主机系统是DCM 6.1.7up3 15284版 网上修改的教程很多,走了好多弯路终于搞定我的黑群NAS,现分享给各位道友,有不足的地方请给位 ...

  6. ESB开发WebService接口

    1 概述 在进行系统间集成时经常利用WebService,但是从建立WebService和调用的重复性和维护性的工作量都相当大. 首先简单介绍一下,ESB全称为Enterprise Service B ...

  7. MapReduce多种join实现实例分析(二)

    上一篇<MapReduce多种join实现实例分析(一)>,大家可以点击回顾该篇文章.本文是MapReduce系列第二篇. 一.在Map端进行连接使用场景:一张表十分小.一张表很大.用法: ...

  8. Javascript高级编程学习笔记(62)—— 事件(6)焦点事件

    焦点事件 焦点事件会在页面元素获得或者失去焦点时触发,利用焦点事件和 document.hasFocus() 方法配合使用 以及 document.activeElement 属性配合可以知晓用户在页 ...

  9. 全栈开发工程师微信小程序-中(中)

    全栈开发工程师微信小程序-中(中) 开放能力 open-data 用于展示微信开放的数据 type 开放数据类型 open-gid 当 type="groupName" 时生效, ...

  10. Core统一日志处理

    新建一个Core的Web项目,然后创建相关文件等 添加一个处理错误的类库ErrorMiddleware   下面是该类库的代码 public class ErrorMiddleware { stati ...