zip-master.zip

  • harishalgat
    了解作者
  • C/C++
    开发工具
  • 118KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-10-21 01:27
    上传日期
I was looking for zip library written in C for my project, but I could not find anything simple enough and lightweight. Everything what I tried required 'crazy mental gymnastics' to integrate or had some limitations or was too heavy. I hate frameworks, factories and adding new dependencies. If I must to install all those dependencies and link new library, I'm getting almost sick. I wanted something powerfull and small enough, so I could add just a few files and compile them into my project. And finally I found miniz. Miniz is a lossless, high performance data compression library in a single source file. I only needed simple interface to append buffers or files to the current zip-entry. Thanks to this feature I'm able to merge many files/buffers and compress them on-the-fly.
zip-master.zip
  • zip-master
  • .github
  • workflows
  • build.yml
    1.2KB
  • test
  • CMakeLists.txt
    230B
  • test.c
    14.5KB
  • src
  • miniz.h
    266.2KB
  • zip.c
    26.4KB
  • zip.h
    10.2KB
  • cmake
  • FindASan.cmake
    2.1KB
  • FindUBSan.cmake
    1.7KB
  • FindMSan.cmake
    2.2KB
  • FindSanitizers.cmake
    3.3KB
  • Config.cmake.in
    125B
  • sanitize-helpers.cmake
    7.2KB
  • FindTSan.cmake
    2.5KB
  • cmake_uninstall.cmake.in
    1.1KB
  • asan-wrapper
    2KB
  • UNLICENSE
    1.2KB
  • Doxyfile.in
    105.7KB
  • zip.png
    5.2KB
  • .gitignore
    537B
  • README.md
    10.3KB
  • CMakeLists.txt
    3.9KB
内容介绍
### A portable (OSX/Linux/Windows), simple zip library written in C This is done by hacking awesome [miniz](https://code.google.com/p/miniz) library and layering functions on top of the miniz v1.15 API. [![Build](https://github.com/kuba--/zip/workflows/build/badge.svg)](https://github.com/kuba--/zip/actions?query=workflow%3Abuild) [![Version](https://badge.fury.io/gh/kuba--%2Fzip.svg)](https://github.com/kuba--/zip/releases) # The Idea ... Some day, I was looking for zip library written in C for my project, but I could not find anything simple enough and lightweight. Everything what I tried required 'crazy mental gymnastics' to integrate or had some limitations or was too heavy. I hate frameworks, factories and adding new dependencies. If I must to install all those dependencies and link new library, I'm getting almost sick. I wanted something powerfull and small enough, so I could add just a few files and compile them into my project. And finally I found miniz. Miniz is a lossless, high performance data compression library in a single source file. I only needed simple interface to append buffers or files to the current zip-entry. Thanks to this feature I'm able to merge many files/buffers and compress them on-the-fly. It was the reason, why I decided to write zip module on top of the miniz. It required a little bit hacking and wrapping some functions, but I kept simplicity. So, you can grab these 3 files and compile them into your project. I hope that interface is also extremely simple, so you will not have any problems to understand it. # Examples * Create a new zip archive with default compression level. ```c struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'w'); { zip_entry_open(zip, "foo-1.txt"); { const char *buf = "Some data here...\0"; zip_entry_write(zip, buf, strlen(buf)); } zip_entry_close(zip); zip_entry_open(zip, "foo-2.txt"); { // merge 3 files into one entry and compress them on-the-fly. zip_entry_fwrite(zip, "foo-2.1.txt"); zip_entry_fwrite(zip, "foo-2.2.txt"); zip_entry_fwrite(zip, "foo-2.3.txt"); } zip_entry_close(zip); } zip_close(zip); ``` * Append to the existing zip archive. ```c struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a'); { zip_entry_open(zip, "foo-3.txt"); { const char *buf = "Append some data here...\0"; zip_entry_write(zip, buf, strlen(buf)); } zip_entry_close(zip); } zip_close(zip); ``` * Extract a zip archive into a folder. ```c int on_extract_entry(const char *filename, void *arg) { static int i = 0; int n = *(int *)arg; printf("Extracted: %s (%d of %d)\n", filename, ++i, n); return 0; } int arg = 2; zip_extract("foo.zip", "/tmp", on_extract_entry, &arg); ``` * Extract a zip entry into memory. ```c void *buf = NULL; size_t bufsize; struct zip_t *zip = zip_open("foo.zip", 0, 'r'); { zip_entry_open(zip, "foo-1.txt"); { zip_entry_read(zip, &buf, &bufsize); } zip_entry_close(zip); } zip_close(zip); free(buf); ``` * Extract a zip entry into memory (no internal allocation). ```c unsigned char *buf; size_t bufsize; struct zip_t *zip = zip_open("foo.zip", 0, 'r'); { zip_entry_open(zip, "foo-1.txt"); { bufsize = zip_entry_size(zip); buf = calloc(sizeof(unsigned char), bufsize); zip_entry_noallocread(zip, (void *)buf, bufsize); } zip_entry_close(zip); } zip_close(zip); free(buf); ``` * Extract a zip entry into memory using callback. ```c struct buffer_t { char *data; size_t size; }; static size_t on_extract(void *arg, unsigned long long offset, const void *data, size_t size) { struct buffer_t *buf = (struct buffer_t *)arg; buf->data = realloc(buf->data, buf->size + size + 1); assert(NULL != buf->data); memcpy(&(buf->data[buf->size]), data, size); buf->size += size; buf->data[buf->size] = 0; return size; } struct buffer_t buf = {0}; struct zip_t *zip = zip_open("foo.zip", 0, 'r'); { zip_entry_open(zip, "foo-1.txt"); { zip_entry_extract(zip, on_extract, &buf); } zip_entry_close(zip); } zip_close(zip); free(buf.data); ``` * Extract a zip entry into a file. ```c struct zip_t *zip = zip_open("foo.zip", 0, 'r'); { zip_entry_open(zip, "foo-2.txt"); { zip_entry_fread(zip, "foo-2.txt"); } zip_entry_close(zip); } zip_close(zip); ``` * List of all zip entries ```c struct zip_t *zip = zip_open("foo.zip", 0, 'r'); int i, n = zip_total_entries(zip); for (i = 0; i < n; ++i) { zip_entry_openbyindex(zip, i); { const char *name = zip_entry_name(zip); int isdir = zip_entry_isdir(zip); unsigned long long size = zip_entry_size(zip); unsigned int crc32 = zip_entry_crc32(zip); } zip_entry_close(zip); } zip_close(zip); ``` * Compress folder (recursively) ```c void zip_walk(struct zip_t *zip, const char *path) { DIR *dir; struct dirent *entry; char fullpath[MAX_PATH]; struct stat s; memset(fullpath, 0, MAX_PATH); dir = opendir(path); assert(dir); while ((entry = readdir(dir))) { // skip "." and ".." if (!strcmp(entry->d_name, ".\0") || !strcmp(entry->d_name, "..\0")) continue; snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entry->d_name); stat(fullpath, &s); if (S_ISDIR(s.st_mode)) zip_walk(zip, fullpath); else { zip_entry_open(zip, fullpath); zip_entry_fwrite(zip, fullpath); zip_entry_close(zip); } } closedir(dir); } ``` # Bindings Compile zip library as a dynamic library. ```shell $ mkdir build $ cd build $ cmake -DBUILD_SHARED_LIBS=true .. $ make ``` ### [Go](https://golang.org) (cgo) ```go package main /* #cgo CFLAGS: -I../src #cgo LDFLAGS: -L. -lzip #include <zip.h> */ import "C" import "unsafe" func main() { path := C.CString("/tmp/go.zip") zip := C.zip_open(path, 6, 'w') entryname := C.CString("test") C.zip_entry_open(zip, entryname) content := "test content" buf := unsafe.Pointer(C.CString(content)) bufsize := C.size_t(len(content)) C.zip_entry_write(zip, buf, bufsize) C.zip_entry_close(zip) C.zip_close(zip) } ``` ### [Rust](https://www.rust-lang.org) (ffi) ```rust extern crate libc; use std::ffi::CString; #[repr(C)] pub struct Zip { _private: [u8; 0], } #[link(name = "zip")] extern "C" { fn zip_open(path: *const libc::c_char, level: libc::c_int, mode: libc::c_char) -> *mut Zip; fn zip_close(zip: *mut Zip) -> libc::c_void; fn zip_entry_open(zip: *mut Zip, entryname: *const libc::c_char) -> libc::c_int; fn zip_entry_close(zip: *mut Zip) -> libc::c_int; fn zip_entry_write( zip: *mut Zip, buf: *const libc::c_void, bufsize: libc::size_t, ) -> libc::c_int; } fn main() { let path = CString::new("/tmp/rust.zip").unwrap(); let mode: libc::c_char = 'w' as libc::c_char; let entryname = CString::new("test.txt").unwrap(); let content = "test content\0"; unsafe { let zip: *mut Zip = zip_open(path.as_ptr(), 5, mode); { zip_entry_open(zip, entryname.as_ptr()); { let buf = content.as_ptr() as *const libc::c_void; let bufsize = content.len() as libc::size_t; zip_entry_write(zip, buf, bufsize); } zip_entry_close(zip); } zip_close(zip); } } ``` ### [Ruby](http://www.ruby-lang.org) (ffi) Install _ffi_ gem. ```shell $ gem install ffi ``` Bind in your module. ```ruby require 'ffi' module Zip extend FFI::Library ffi_lib "./libzip.#{::FFI::Platform::LIBSUFFIX}" attach_function :zip_open, [:string, :int, :char], :pointer attach_function :zip_close, [:pointer], :void attach_function :zip_entry_open, [:pointer, :string], :int attach_function :zip_entry_close, [:pointer], :
评论
    相关推荐
    • What-is-BCI.rar
      Introduction to Brain Computer Interfacing (BCI).
    • peace_essay.ZIP
      This is English language what
    • abdkok_tabINDEX02072005.zip
      I did not write this code, what I did was get examples from http://www.planet-source-code.com * put them all together and manage to produce this sample: * Credit should go to the developers who ...
    • what-do-you-see-master.zip
      看你所见 ...所以你不开心的你看到了什么! 干得好,欢迎来到揭开秘密的地方。 这是 Digital Contraptions Imaginarium 2014 年数字圣诞卡的样子: ...... (如果您愿意我也用于在智能手机上显示的) ...
    • What-is-a-quantum-field-state--master.zip
      什么是量子场态? 介绍 这是开放科学中的另一项实验。 您在这里看到的是我们正在写的一篇论文的当前乳胶来源,回答了这个问题:“什么是量子场态?”。 本文已“大部分完成”,即基本概念和构造已完成,但仍有许多...
    • VBJSON.zip
      vbjson string filter
    • MachineLearningAllMaterials.zip
      MachineLearningAllMaterials
    • zip-master.zip
      c++ zip master library
    • phgfinfo.zip
      Wow if I even told you what was in here your small brain would probably explode, so I'd better not...
    • lzw_soft.zip
      lzw压缩解压算法源码