1、.gz文件是linux下常见的压缩格式。使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream

     public static void unGzipFile(String sourcedir) {
String ouputfile = "";
try {
//建立gzip压缩文件输入流
FileInputStream fin = new FileInputStream(sourcedir);
//建立gzip解压工作流
GZIPInputStream gzin = new GZIPInputStream(fin);
//建立解压文件输出流
ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.'));
ouputfile = ouputfile.substring(0,ouputfile.lastIndexOf('.'));
FileOutputStream fout = new FileOutputStream(ouputfile); int num;
byte[] buf=new byte[1024]; while ((num = gzin.read(buf,0,buf.length)) != -1)
{
fout.write(buf,0,num);
} gzin.close();
fout.close();
fin.close();
} catch (Exception ex){
System.err.println(ex.toString());
}
return;
}

2、zip文件,使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile

 /**
* 解压缩zipFile
* @param file 要解压的zip文件对象
* @param outputDir 要解压到某个指定的目录下
* @throws IOException
*/
public static void unZip(File file,String outputDir) throws IOException {
ZipFile zipFile = null; try {
Charset CP866 = Charset.forName("CP866"); //specifying alternative (non UTF-8) charset
//ZipFile zipFile = new ZipFile(zipArchive, CP866);
zipFile = new ZipFile(file, CP866);
createDirectory(outputDir,null);//创建输出目录 Enumeration<?> enums = zipFile.entries();
while(enums.hasMoreElements()){ ZipEntry entry = (ZipEntry) enums.nextElement();
System.out.println("解压." + entry.getName()); if(entry.isDirectory()){//是目录
createDirectory(outputDir,entry.getName());//创建空目录
}else{//是文件
File tmpFile = new File(outputDir + "/" + entry.getName());
createDirectory(tmpFile.getParent() + "/",null);//创建输出目录 InputStream in = null;
OutputStream out = null;
try{
in = zipFile.getInputStream(entry);;
out = new FileOutputStream(tmpFile);
int length = 0; byte[] b = new byte[2048];
while((length = in.read(b)) != -1){
out.write(b, 0, length);
} }catch(IOException ex){
throw ex;
}finally{
if(in!=null)
in.close();
if(out!=null)
out.close();
}
}
} } catch (IOException e) {
throw new IOException("解压缩文件出现异常",e);
} finally{
try{
if(zipFile != null){
zipFile.close();
}
}catch(IOException ex){
throw new IOException("关闭zipFile出现异常",ex);
}
}
} /**
* 构建目录
* @param outputDir
* @param subDir
*/
public static void createDirectory(String outputDir,String subDir){
File file = new File(outputDir);
if(!(subDir == null || subDir.trim().equals(""))){//子目录不为空
file = new File(outputDir + "/" + subDir);
}
if(!file.exists()){
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
file.mkdirs();
}
}

3、.tar.gz文件可以看做先用tar打包,再使用gz进行压缩。

使用org.apache.tools.tar.TarEntry;  org.apache.tools.tar.TarInputStream 和 org.apache.tools.tar.TarOutputStream

  //------------------------------------------------------------------------------------------------------
/**
* 解压tar.gz 文件
* @param file 要解压的tar.gz文件对象
* @param outputDir 要解压到某个指定的目录下
* @throws IOException
*/
public static void unTarGz(File file,String outputDir) throws IOException{
TarInputStream tarIn = null;
try{
tarIn = new TarInputStream(new GZIPInputStream(
new BufferedInputStream(new FileInputStream(file))),
1024 * 2); createDirectory(outputDir,null);//创建输出目录 TarEntry entry = null;
while( (entry = tarIn.getNextEntry()) != null ){ if(entry.isDirectory()){//是目录
entry.getName();
createDirectory(outputDir,entry.getName());//创建空目录
}else{//是文件
File tmpFile = new File(outputDir + "/" + entry.getName());
createDirectory(tmpFile.getParent() + "/",null);//创建输出目录
OutputStream out = null;
try{
out = new FileOutputStream(tmpFile);
int length = 0; byte[] b = new byte[2048]; while((length = tarIn.read(b)) != -1){
out.write(b, 0, length);
} }catch(IOException ex){
throw ex;
}finally{ if(out!=null)
out.close();
}
}
}
}catch(IOException ex){
throw new IOException("解压归档文件出现异常",ex);
} finally{
try{
if(tarIn != null){
tarIn.close();
}
}catch(IOException ex){
throw new IOException("关闭tarFile出现异常",ex);
}
}
}

使用到的包头有:

 import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.apache.tools.tar.TarOutputStream;

最新文章

  1. 新手如何查看API文档?
  2. ECharts-基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表
  3. Codeforces Round #370(div 2)
  4. 跟着百度学PHP[4]OOP面对对象编程-6-封装性private
  5. 高度平衡的二叉搜索树(AVL树)
  6. Redis学习手册(Sorted-Sets数据类型)
  7. Tomcat JMX
  8. python 堆排序
  9. EventBus3.0 study
  10. Manifest文件的最新理解
  11. Spring-cloud (九) Hystrix请求合并的使用
  12. flink 学习
  13. Project Structure详解
  14. python技巧 python2中的除法结果为0
  15. AT91RM9200---电源管理控制器(PMC)介绍
  16. 前端基础——css
  17. 移动设备&#160;小米2S不显示CD驱动器(H),便携设备,MTP,驱动USB&#160;Driver,MI2感叹号的解决方法
  18. Oracle简单的备份和恢复-导入和导出-目录
  19. 面试的角度诠释Java工程师(一)
  20. CentOS工作内容(四)主机禁ping

热门文章

  1. javascript的一些札记
  2. shiro 实现 用户 a 操作b 的权限 ,用户 b 能够及时获知。b不需要退出登陆 。 关闭鉴权缓存,或者不配置缓存
  3. 用Notepad++在文本文件里快速在每行头尾都加上指定的内容(转载)
  4. java Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: main (wrong name: pm/main)
  5. vs2010 EF4.0 访问mysql
  6. C++之new/delete/malloc/free详解
  7. Unable to load tag handler class &quot;com.showId.Id.ShowId&quot; for tag &quot;ShowId:ShowId&quot;] with root cause错误的解决方案
  8. cordova3.X-4.0添加自定义插件方法
  9. hdu 5037 周期优化
  10. Android-Thread线程的状态