Code Monkey home page Code Monkey logo

Comments (6)

kuba-- avatar kuba-- commented on June 10, 2024

It will require many small changes in minizip, so if your files are not super big you can read them into memory and use zip_stream_... API

from zip.

bruenor41 avatar bruenor41 commented on June 10, 2024

from zip.

kuba-- avatar kuba-- commented on June 10, 2024

Yeah, definitivelly ( it does not sound like a big archive). You can do sth. like:
https://github.com/kuba--/zip/blob/master/test/test_entry.c#L176

from zip.

bruenor41 avatar bruenor41 commented on June 10, 2024

from zip.

kuba-- avatar kuba-- commented on June 10, 2024

I assume you already have a file descriptor where you want to save the zip stream.
Then:

        // create zip stream in memory
	struct zip_t *zip = zip_stream_open(NULL, 0, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');

        // write some data
	zip_entry_open(zip, "entry1.txt");
	const char *txt = "This is content for entry1.txt\0";
	zip_entry_write(zip, txt, strlen(txt));
	zip_entry_close(zip);

        // get zip stream into the buffer
	void *buf = NULL;
	size_t bufsize = 0;
	zip_stream_copy(zip, (void **)&buf, &bufsize);

	zip_stream_close(zip);

        // we should already have a file descriptor
	int fd = open("/tmp/file.zip", O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR | S_IWUSR);
        // write zip stream to your file descriptor
	write(fd, buf, bufsize);
	close(fd);

        // remember to free memory allocated for the buffer!
	free(buf);
$ zipinfo /tmp/file.zip

Archive:  /tmp/file.zip
Zip file size: 179 bytes, number of entries: 1
-rw----     0.0 fat       30 bX defN 23-Mar-19 13:36 entry1.txt
1 file, 30 bytes uncompressed, 29 bytes compressed:  3.3%


$ unzip /tmp/file.zip 
$ cat entry1.txt 
This is content for entry1.txt%  

Keep in mind - there is no error checking in above example.

Good luck!

from zip.

bruenor41 avatar bruenor41 commented on June 10, 2024

from zip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.