简体   繁体   中英

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. 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

I need to read and process data from .dat file .

You're reading the tar file as gzip compressed while it's plain tar and only the items inside are compressed.

So avoid this GZIPInputStream and instead go item by item, read it using read() and then process with 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 . But from the filename it seems you have a plain tar archive.

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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM