简体   繁体   English

java.util.zip.ZipException:不是GZIP格式-commons-compress.jar

[英]java.util.zip.ZipException: Not in GZIP format - commons-compress.jar

I am trying to uncompress the tar files using commons-compress.jar for the first time. 我正在尝试第一次使用commons-compress.jar解压缩tar文件。 Here is my initial code which is throwing the error. 这是引发错误的我的初始代码。

TarArchiveInputStream myTarFile=new TarArchiveInputStream(
(new GZIPInputStream
(new FileInputStream("C:/Users/abc/xyz_20151010.tar")));
System.out.println(myTarFile.getCurrentEntry());

The tar file has set of files with extension .dat.gz.bak tar文件包含扩展名为.dat.gz.bak的文件集

I need to read and process data from .dat file . 我需要从.dat文件读取和处理数据。

You're reading the tar file as gzip compressed while it's plain tar and only the items inside are compressed. 您正在将tar文件读为gzip压缩文件,而它是纯tar文件,并且仅压缩其中的项目。

So avoid this GZIPInputStream and instead go item by item, read it using read() and then process with GZIPInputstream(ByteArrayInputStream(content)). 因此,请避免使用此GZIPInputStream,而应逐项进行操作,使用read()进行读取,然后使用GZIPInputstream(ByteArrayInputStream(content))进行处理。 You may consider creating input stream which reads the content on the fly. 您可以考虑创建输入流,以即时读取内容。

If your input file were a .tar.gz you should have wrapped the file in a TarInputStream , and the tar in a GZip . 如果输入文件是.tar.gz ,则应将文件包装在TarInputStream ,而tar包装在GZip But from the filename it seems you have a plain tar archive. 但是从文件名看来,您似乎有一个普通的tar存档。

So, if I understand your input format, you need something like this: 因此,如果我了解您的输入格式,则需要这样的内容:

public class MyDataReader {

  private final TarArchiveInputStream tar;

  public boolean hasNextData() {
    return tar.getNextTarEntry() != null;
  }

  public MyData nextData() {
    byte[] buff = new byte[tar.getCurrentEntry().getSize()];
    // loop over tar until all entry has been read
    InputStream entry = new ByteArrayInputStream(buff);
    GZIPInputStream gzip = new GZipInputStream(entry);
    // process gzip input stream
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.util.zip.ZIPException: 不是 GZIP 格式 - java.util.zip.ZIPException: Not in GZIP format 解决方法:java.util.zip.ZipException: Not in GZIP format - How to resolve: java.util.zip.ZipException: Not in GZIP format 解释来自 API 的 gzip 响应正文时出现错误:“java.util.zip.ZipException:不是 GZIP 格式” - Error: "java.util.zip.ZipException: Not in GZIP format" when interpreting gzip response body from API java.util.zip.ZipException:尝试下载远程 zip 文件时不是 GZIP 格式 - java.util.zip.ZipException: Not in GZIP format while trying to download a remote zip file java.util.zip.ZipException:使用jnlp启动Java Web Start应用程序时,不是GZIP格式的异常 - java.util.zip.ZipException: Not in GZIP format exception when launching java web start app with jnlp 修复 java.util.zip.ZipException - fixing java.util.zip.ZipException 关于Retrofit和GSON的java.util.zip.ZipException - java.util.zip.ZipException on Retrofit and GSON java.util.zip.ZipException:打开zip文件时出错 - java.util.zip.ZipException: error in opening zip file 将 jar 文件从 Windows 7 复制到 Unix 会产生 java.util.zip.ZipException - Copying a jar file from Windows 7 to Unix gives java.util.zip.ZipException java.util.zip.ZipException:打开 zip 文件时出错 - java.util.zip.ZipException: error in opening zip file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM