Code Monkey home page Code Monkey logo

nozip's Introduction

nozip

nozip is a simple deflated zip decompressor written in C.

  • Amalgamated source code
  • Simple api (check nozip.h)
  • Small size (~18kb dependency)

Usage Example

CMake:

add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/deps/nozip")
add_executable(app main.c)
target_link_libraries(app nozip::libnozip)

File read:

#include <stdio.h>
#include <stdlib.h>

#include "nozip.h"

int main(int argc, char **argv)
{
	FILE *fp;
	nozip_t *zip;
	size_t size;
	size_t num_entries;

	if (argc < 2) {
		fprintf(stderr, "usage: %s file\n", argv[0]);
		return 1;
	}

	if (!(fp = fopen(argv[1], "rb"))) {
		perror(argv[1]);
		return 1;
	}

	nozip_t *zip = malloc(nozip_size(fp)); 
	if (!zip || !(num_entries = nozip_read(zip, fp))) {
		perror(argv[1]);
		return 1;
	}

	entries = nozip_entries(zip);
	printf("entries:\n");
	for (size_t i = 0; i < num_entries; ++i) {
		entry = entries + i;
		output = malloc(entry->uncompressed_size);
		if (nozip_uncompress(zip, entry, output, entry->uncompressed_size) != entry->uncompressed_size)
		{
			// failure
			free(output);
			free(zip);
			return 1;
		}
		printf("%s\n", entry->filename);
		// utilize output
		free(output);
	}

	free(zip);

	return 0;
}

Memory read:

#include <stdio.h>
#include <stdlib.h>

#include "nozip.h" 

extern const unsigned char data[];
extern const unsigned int  data_length;

int main(int argc, const char **argv)
{
	nozip_t *zip;
	nozip_entry_t *entries, entry;
	size_t num_entries;

	zip = malloc(nozip_size_mem((void*)data, data_length)); 
	if (!zip || !(num_entries = nozip_read_mem(zip, (void*)data, data_length))) {
		return 1;
	}
	entries = nozip_entries(zip);
	printf("entries:\n");
	for (size_t i = 0; i < num_entries; ++i) {
		entry = entries + i;
		output = malloc(entry->uncompressed_size);
		if (nozip_uncompress(zip, entry, output, entry->uncompressed_size) != entry->uncompressed_size)
		{
			// failure
			free(output);
			free(zip);
			return 1;
		}
		printf("%s\n", entry->filename);
		// utilize output
		free(output);
	}

	free(zip);

	return 0;
}

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.