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

  • Home
  • SEARCH
  • 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 7510803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:20:11+00:00 2026-05-29T23:20:11+00:00

I’m building an app which requires reading a file inside a zipfile without extracting

  • 0

I’m building an app which requires reading a file inside a zipfile without extracting the file. Is there any library out there with this kind of function or can you give me some ideas on how to solve it?

  • 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-05-29T23:20:13+00:00Added an answer on May 29, 2026 at 11:20 pm

    You can unzip one buffer-ful of data at a time, and read through that, assuming that the file is text and is structured in a way that you can parse it incrementally (for example, XML data that is processed with a SAX parser).

    Here is a routine I wrote that extracts data from a zip-stream and prints out a line at a time. You could modify it to print out or parse N characters, instead. Or you could add the lines to a line buffer, and process/parse a chunk of lines at once.

    This uses zlib, and if this code is useful to you, feel free to use it:

    #
    # compile with -lz option in order to link in the zlib library
    #
    
    #include <zlib.h>
    
    #define Z_CHUNK 2097152
    
    int unzipFile(const char *fName) 
    {
        z_stream zStream;
        char *zRemainderBuf = malloc(1);
        unsigned char zInBuf[Z_CHUNK];
        unsigned char zOutBuf[Z_CHUNK];
        char zLineBuf[Z_CHUNK];
        unsigned int zHave, zBufIdx, zBufOffset, zOutBufIdx;
        int zError;
        FILE *inFp = fopen(fName, "rbR");
    
        if (!inFp) { fprintf(stderr, "could not open file: %s\n", fName); return EXIT_FAILURE; }
    
        zStream.zalloc = Z_NULL;
        zStream.zfree = Z_NULL;
        zStream.opaque = Z_NULL;
        zStream.avail_in = 0;
        zStream.next_in = Z_NULL;  
    
        zError = inflateInit2(&zStream, (15+32)); /* cf. http://www.zlib.net/manual.html */
        if (zError != Z_OK) { fprintf(stderr, "could not initialize z-stream\n"); return EXIT_FAILURE; }
    
        *zRemainderBuf = '\0';
        do {
            zStream.avail_in = fread(zInBuf, 1, Z_CHUNK, inFp);
            if (zStream.avail_in == 0)
                break;
            zStream.next_in = zInBuf;
            do {
                zStream.avail_out = Z_CHUNK;
                zStream.next_out = zOutBuf;
                zError = inflate(&zStream, Z_NO_FLUSH);
                switch (zError) {
                    case Z_NEED_DICT:  { fprintf(stderr, "Z-stream needs dictionary!\n"); return EXIT_FAILURE; }
                    case Z_DATA_ERROR: { fprintf(stderr, "Z-stream suffered data error!\n"); return EXIT_FAILURE; }
                    case Z_MEM_ERROR:  { fprintf(stderr, "Z-stream suffered memory error!\n"); return EXIT_FAILURE; }
                }
                zHave = Z_CHUNK - zStream.avail_out;
                zOutBuf[zHave] = '\0';
    
                /* copy remainder buffer onto line buffer, if not NULL */
                if (zRemainderBuf) {
                    strncpy(zLineBuf, zRemainderBuf, strlen(zRemainderBuf));
                    zBufOffset = strlen(zRemainderBuf);
                }
                else
                    zBufOffset = 0;
    
                /* read through zOutBuf for newlines */
                for (zBufIdx = zBufOffset, zOutBufIdx = 0; zOutBufIdx < zHave; zBufIdx++, zOutBufIdx++) {
                    zLineBuf[zBufIdx] = zOutBuf[zOutBufIdx];
                    if (zLineBuf[zBufIdx] == '\n') {
                        zLineBuf[zBufIdx] = '\0'; 
                        zBufIdx = -1;
                        fprintf(stdout, "%s\n", zLineBuf);
                    }
                }
    
                /* copy some of line buffer onto the remainder buffer, if there are remnants from the z-stream */
                if (strlen(zLineBuf) > 0) {
                    if (strlen(zLineBuf) > strlen(zRemainderBuf)) {
                        /* to minimize the chance of doing another (expensive) malloc, we double the length of zRemainderBuf */
                        free(zRemainderBuf);
                        zRemainderBuf = malloc(strlen(zLineBuf) * 2);
                    }
                    strncpy(zRemainderBuf, zLineBuf, zBufIdx);
                    zRemainderBuf[zBufIdx] = '\0';
                }
            } while (zStream.avail_out == 0);
        } while (zError != Z_STREAM_END);
    
        /* close gzip stream */
        zError = inflateEnd(&zStream);
        if (zError != Z_OK) { 
            fprintf(stderr, "could not close z-stream!\n");
            return EXIT_FAILURE;
        }
        if (zRemainderBuf)
            free(zRemainderBuf);
    
        fclose(inFp);
    
        return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the

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.