Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:56:58+00:00 2026-06-14T17:56:58+00:00

I’ve written a rest resource that serves a .tar.gz file. It’s working OK. I’ve

  • 0

I’ve written a rest resource that serves a .tar.gz file. It’s working OK. I’ve tried requesting it, saving the data, unpacking it (with tar xzvf [filename]) and I get the correct data.

However, I’m trying to use java.util.zip.GZIPInputStream and org.apache.tools.tar.TarInputStream to unzip and untar a .tar.gz that I’m serving in a JUnit test, to verify that it’s working automatically. This is the code in my unit test with some details removed:

    HttpResponse response = <make request code here>
    byte[] receivedBytes = FileHelper.copyInputStreamToByteArray(response.getEntity().getContent(), true);

    GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(receivedBytes));
    TarInputStream tarInputStream = new TarInputStream(gzipInputStream);
    TarEntry tarEntry = tarInputStream.getNextEntry();
    ByteArrayOutputStream byteArrayOutputStream = null;
    System.out.println("Record size: " + tarInputStream.getRecordSize());
    while (tarEntry != null) // It only goes in here once
    {
        byteArrayOutputStream = new ByteArrayOutputStream();
        tarInputStream.copyEntryContents(byteArrayOutputStream);
        tarEntry = tarInputStream.getNextEntry();
    }
    byteArrayOutputStream.flush();
    byteArrayOutputStream.close();
    byte[] archivedBytes = byteArrayOutputStream.toByteArray();
    byte[] actualBytes = <get actual bytes>
    Assert.assertArrayEquals(actualBytes, archivedBytes);

The final assert fails with a difference at byte X = (n * 512) + 1, where n is the greatest natural number such that n * 512 <= l and l is the length of the data. That is, I get the the biggest possible multiple of 512 bytes of data correctly, but debugging the test I can see that all the remaining bytes are zero. So, if the total amount of data is 1000 bytes, the first 512 bytes in archivedBytes are correct, but the last 488 are all zero / unset, and if the total data is 262272 bytes I get the first 262144 (512*512) bytes correctly, but the remaining bytes are all zero again.

Also, the tarInputStream.getRecordSize() System out above prints Record size: 512, so I presume that this is somehow related. However, since the archive works if I download it, I guess the data must be there, and there’s just something I’m missing.

Stepping into the tarInputStream.copyEntryContents(byteArrayOutputStream) with the 1000 byte data, in

int numRead = read(buf, 0, buf.length);

the numRead is 100, but looking at the buffer, only the first 512 bytes are non-zero. Maybe I shouldn’t be using that method to get the data out of the TarInputStream?

If anyone knows how it’s supposed to work, I’d be very grateful for any advice or help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:56:59+00:00Added an answer on June 14, 2026 at 5:56 pm

    It turned out that I was wrong in my original question, and the error was in the resource code. I wasn’t closing the entry on the TarOutputStream when writing to it. I guess this was not causing any problems when requesting it manually from the server, maybe because the entry was closed with the connection or something, but working differently when being requested from a Unit test… though I must admit that doesn’t make a whole lot of sense to be 😛

    Looking at the fragment of my writing code below, I was missing line 3.

    1: tarOutputStream.putNextEntry(tarEntry);
    2: tarOutputStream.write(fileRawBytes);
    3: tarOutputStream.closeEntry();
    4: tarOutputStream.close();
    

    I didn’t even know there was such a thing as a “closeEntry” on the TarOutputStream… I do now! 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.