java 如何用zlib解压缩tar.gz文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 如何用zlib解压缩tar.gz文件相关的知识,希望对你有一定的参考价值。

参考技术A public static void makeZip(List<File> fileList,String zipPath,boolean isDelete)
byte[] buf = new byte[1024];
try
// Create the ZIP file
File zipFile = new File(zipPath);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
// Compress the files
for (int i = 0; i < fileList.size(); i++)
FileInputStream in = new FileInputStream(fileList.get(i));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileList.get(i).getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0)
out.write(buf, 0, len);

// Complete the entry
out.closeEntry();
in.close();

// Complete the ZIP file
out.close();
System.out.println("压缩完成.");

//把旧的文件删除
if(isDelete == true)
for (int i = 0; i < fileList.size(); i++)
File oldFile = fileList.get(i);
oldFile.delete();



catch (IOException e)
e.printStackTrace();


public static void main(String[] args)
File in1=new File("D:\\a.txt");
File in2=new File("D:\\b.txt");
File[] file=new File[]in1,in2;
File zip=new File("D:\\ab.zip");
IDMZip mgr=new IDMZip();
mgr.ZipFiles(file, zip);


这个方法不管你是在windows下还是在linux下,都能正常执行。追问

谢谢,但是我是要解压这样20140718_185819.data.tar.gz 不是zip的

追答

你可以试试。还有这个方法。都是我项目里曾经用到过的。都是可用的。
public static List unZip(String path) throws FileNotFoundException,
IOException
path = path.replaceAll("\\\\","/");
String zipPath = path.substring(0,path.lastIndexOf("/")+1);
List xmlFileNameList = new ArrayList();
byte[] data = new byte[1024*2];
FileInputStream fis = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = null;
while((entry = zis.getNextEntry())!= null)
if(entry.isDirectory())
File file = new File(zipPath+entry.getName());
file.mkdirs();
continue;

本回答被提问者采纳

以上是关于java 如何用zlib解压缩tar.gz文件的主要内容,如果未能解决你的问题,请参考以下文章

如何解压.tar.gz gzip gz 类型文档

linux下几种压缩解压缩方法

如何只查看tar.gz压缩文件中顶层目录的列表

Ubuntu中的解压缩文件的方式

kali linux 压缩文件解压缩命令(包含7z)

ubuntu下文件压缩解压缩命令