Zlib iPhone - files get shit in the beginning

I have several .tgz files in my bundle that I want to unpack and write to a file. It works for me - like. The problem is that the written file gets 512 bytes of crap data in front of it, but other than that, the file is successfully decompressed.

alt text
(source: pici.se )

I don't want shit. If it's always 512 bytes, of course, it's easy to skip those and write others. But is it always so? It's risky to do something like this if no one knows why those bytes even exist.

    gzFile f = gzopen ([[[NSBundle mainBundle] pathForResource:file ofType:@"tgz"] cStringUsingEncoding:NSASCIIStringEncoding], [@"rb" cStringUsingEncoding:NSASCIIStringEncoding]); 
    unsigned int length = 1024*1024;
    void *buffer = malloc(length);
    NSMutableData *data = [NSMutableData new];

    while (true)
    {   
        int read = gzread(f, buffer, length);

        if (read > 0)
        {
            [data appendBytes:buffer length:read];
        }
        else if (read == 0)
            break;
        else if (read == -1)
        {
            throw [NSException exceptionWithName:@"Decompression failed" reason:@"read = -1" userInfo:nil];
        }
        else
        {
            throw [NSException exceptionWithName:@"Unexpected state from zlib" reason:@"read < -1" userInfo:nil];
        }
    }

    int writeSucceeded = [data writeToFile:file automatically:YES];

    free(buffer);
    [data release];

    if (!writeSucceeded)
        throw [NSException exceptionWithName:@"Write failed" reason:@"writeSucceeded != true" userInfo:nil];

      

+2


source to share


2 answers


Based on the code you posted, it looks like you are trying to read a tarzed gZip'ed file using only gzip.

I am assuming that the "garbage" at the beginning of the file after decompression is the header of the TAR file (I see the file name there right at the beginning).

Other Hints The tar file format indicates 512 bytes.

gzip can compress only one file. If you're only trying to compress one file, you don't need to warp it first.



If you are trying to compress multiple files and as one archive, you will need to use TAR and unpack the files after unpacking them.

Just guess.

Chris.

+6


source


It looks like a sane implementation. Have you tried unpacking the TGZ with a known good tool (i.e. Tar -xzf) and see if it works fine?



+1


source







All Articles