List Archive

Thread

Thread Index

Message

From: Lisa Matias <lisa.matias%gmail.com@localhost>
To: Thomas Klausner <tk%giga.or.at@localhost>
Subject: Re: ZIP_CREATE flag for zip_fdopen issues
Date: Sun, 8 Jan 2017 12:01:54 -0800

Sorry Thomas that was a typo on my part.

Please consider this first complete standalone test program, which creates a "test.zip" file archive, which compiles without errors and works.

// =================================================================
#include <string.h>
#include <stdio.h>
#include <zip.h>

int main (int argc, char **argv) {
    static char file1 [] = "This is the contents of the first file!\n";
    static char file2 [] = "This is the contents of the second file!\n";
    static char file3 [] = "This is the contents of the third file!\n";
    zip_source_t *zipFile;
    zip_t *zip;

    int zipError;

    if (!(zip = zip_open ("test.zip", ZIP_TRUNCATE | ZIP_CREATE, &zipError)))
        printf ("zip_open() error\n");
    else if (!(zipFile = zip_source_buffer (zip, file1, sizeof (file1) - 1, 0)))
        printf ("zip_source_buffer() error\n");
    else if (0 != zip_file_add (zip, "first.txt", zipFile, ZIP_FL_ENC_UTF_8))
        printf ("zip_file_add() error\n");
    else if (!(zipFile = zip_source_buffer (zip, file2, sizeof (file2) - 1, 0)))
        printf ("zip_source_buffer() error!\n");
    else if (1 != zip_file_add (zip, "second.txt", zipFile, ZIP_FL_ENC_UTF_8))
        printf ("zip_file_add() error\n");
    else if (!(zipFile = zip_source_buffer (zip, file3, sizeof (file3) - 1, 0)))
        printf ("zip_source_buffer() error\n");
    else if (2 != zip_file_add (zip, "third.txt", zipFile, ZIP_FL_ENC_UTF_8))
        printf ("zip_file_add() error!\n");
    else {
        zip_close (zip);
        printf ("zip created in file\n");
    }
    return 0;
}
// =================================================================

With the first program, after running it, I get the output:
zip created in file
and I also get a newly created "test.zip" file, which I am able to unzip and successfully extract the 3 archived files ("first.txt", "second.txt", & "third.txt") with the exact same contents I specified, so it does work.

This second complete standalone test program, which tries to create the zip file in memory does not work.

// =================================================================
#include <string.h>
#include <stdio.h>
#include <zip.h>

int main (int argc, char **argv) {
    static char file1 [] = "This is the contents of the first file!\n";
    static char file2 [] = "This is the contents of the second file!\n";
    static char file3 [] = "This is the contents of the third file!\n";
    zip_source_t *zipFile;
    zip_t *zip;

    static unsigned char zipBuffer [65536];  // 64kB buffer for ZIP which is more than large enough
    zip_error_t zipError;
    zip_source_t *zipSource;
    int i;

    memset (zipBuffer, 0, sizeof (zipBuffer));
    zip_error_init (&zipError);
    if (!(zipSource = zip_source_buffer_create (zipBuffer, sizeof (zipBuffer), 0, &zipError)))
        printf ("zip_source_buffer_create() error\n");
    else if (!(zip = zip_open_from_source (zipSource, ZIP_TRUNCATE | ZIP_CREATE, &zipError)))
        printf ("zip_open_from_source() error\n");
    else if (!(zipFile = zip_source_buffer (zip, file1, sizeof (file1) - 1, 0)))
        printf ("zip_source_buffer() error\n");
    else if (0 != zip_file_add (zip, "first.txt", zipFile, ZIP_FL_ENC_UTF_8))
        printf ("zip_file_add() error\n");
    else if (!(zipFile = zip_source_buffer (zip, file2, sizeof (file2) - 1, 0)))
        printf ("zip_source_buffer() error!\n");
    else if (1 != zip_file_add (zip, "second.txt", zipFile, ZIP_FL_ENC_UTF_8))
        printf ("zip_file_add() error\n");
    else if (!(zipFile = zip_source_buffer (zip, file3, sizeof (file3) - 1, 0)))
        printf ("zip_source_buffer() error\n");
    else if (2 != zip_file_add (zip, "third.txt", zipFile, ZIP_FL_ENC_UTF_8))
        printf ("zip_file_add() error!\n");
    else {
        zip_close (zip);
        printf ("zip created in memory\n");
        for (i = 0; i < sizeof (zipBuffer); i++)
            if (zipBuffer [i])
                break;
        if (i < sizeof (zipBuffer))
            printf ("non-zero byte found in zip buffer at i=%d\n", i);
        else
            printf ("zip buffer is all zero\n");
    }
    zip_error_fini (&zipError);
    return 0;
}
// =================================================================
 
Note that I have highlighted the differences between the working file version and not working memory version.

When I run the second program, I get the output:
zip created in memory
zip buffer is all zero

Any assistance to get the second program to work is greatly appreciated.

Thanks.

Lisa.


On 8 January 2017 at 06:24, Thomas Klausner <tk%giga.or.at@localhost> wrote:
Hi Lisa!


On Thu, Jan 05, 2017 at 06:37:42AM -0800, Lisa Matias wrote:
> The RAM disk option is not possible since for security, the CGI web
> application runs in chroot() set to an isolated directory with no other
> access to the web server file-system, so it cannot access /tmp.
> Furthermore, I do not have root/administer access on the web server this
> will be deployed on to set up any other RAM disks.

Ok.

> The example/in-memory.c is different than my intended usage, so I am still
> unclear on how to do this properly.

I think it fits your intended use case quite well. You create the file
in memory, you only have to add code to write the memory to the file
descriptor.


> Please consider this simple code example...

This is really hard because it obviously cannot compile.

> // #########################################
> static char file1 [] = "This is the contents of the first file!";
> static char file2 [] = "This is the contents of the second file!";
> static char file3 [] = "This is the contents of the third file!";
>
> static unsigned char zipBuffer [65536];  // 64kB buffer for ZIP which is
> more than large enough
> zip_source_t *zipSource, *zipFile;
> zip_error_t zipError;
> zip_t *zip;
>
> memset (zipBuffer, 0, sizeof (zipBuffer));
> *zip_error_init (&zipError);*
> if (!(*zipSource = zip_source_buffer_create (zipBuffer, sizeof (zipBuf), 0,
> &zipError)*))

In particular, sizeof(zipBuf) is not the size of zipBuffer.

If you want help, please provide the exact code. But I think debugging
of your code yourself should get you there...

Cheers,
 Thomas

Made by MHonArc.