Skip to content

Commit

Permalink
closes assimp#3831 : update zip
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkulling committed Apr 30, 2021
1 parent 96d0524 commit feeb89a
Show file tree
Hide file tree
Showing 7 changed files with 1,219 additions and 377 deletions.
58 changes: 0 additions & 58 deletions contrib/zip/.gitignore

This file was deleted.

44 changes: 29 additions & 15 deletions contrib/zip/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.4)

project(zip
LANGUAGES C
VERSION "0.1.18")
VERSION "0.1.19")
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

set(CMAKE_VERBOSE_MAKEFILE ON)
option(CMAKE_DISABLE_TESTING "Disable test creation" OFF)

if (MSVC)
# Use secure functions by default and suppress warnings about "deprecated" functions
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -Werror -pedantic")
endif (MSVC)

# zip
set(SRC src/miniz.h src/zip.h src/zip.c)
add_library(${PROJECT_NAME} ${SRC})

# this is the "object library" target: compiles the sources only once
add_library(OBJLIB OBJECT ${SRC})
# shared libraries need PIC
set_property(TARGET OBJLIB PROPERTY POSITION_INDEPENDENT_CODE 1)

# static and shared libraries built from the same object files
if (BUILD_SHARED_LIBS)
add_library(${PROJECT_NAME} SHARED $<TARGET_OBJECTS:OBJLIB>)
include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME})
else()
add_library(${PROJECT_NAME} STATIC $<TARGET_OBJECTS:OBJLIB>)
endif()

target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>
Expand All @@ -34,6 +37,17 @@ if (NOT CMAKE_DISABLE_TESTING)
add_sanitizers(${PROJECT_NAME} ${test_out})
endif()

if (MSVC)
# Use secure functions by default and suppress warnings about "deprecated" functions
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -Werror -pedantic -Wno-deprecated")
endif (MSVC)

####
# Installation (https://github.com/forexample/package-example) {

Expand Down
149 changes: 132 additions & 17 deletions contrib/zip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
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
Expand Down Expand Up @@ -155,10 +154,52 @@ struct zip_t *zip = zip_open("foo.zip", 0, 'r');
zip_close(zip);
```
* Create a new zip archive in memory (stream API).
```c
char *outbuf = NULL;
size_t outbufsize = 0;
const char *inbuf = "Append some data here...\0";
struct zip_t *zip = zip_stream_open(NULL, 0, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
{
zip_entry_open(zip, "foo-1.txt");
{
zip_entry_write(zip, inbuf, strlen(inbuf));
}
zip_entry_close(zip);
/* copy compressed stream into outbuf */
zip_stream_copy(zip, (void **)&outbuf, &outbufsize);
}
zip_stream_close(zip);
free(outbuf);
```

* Extract a zip entry into a memory (stream API).

```c
char *buf = NULL;
ssize_t bufsize = 0;

struct zip_t *zip = zip_stream_open(zipstream, zipstreamsize, 0, 'r');
{
zip_entry_open(zip, "foo-1.txt");
{
zip_entry_read(zip, (void **)&buf, &bufsize);
}
zip_entry_close(zip);
}
zip_stream_close(zip);

free(buf);
```
* List of all zip entries
```c
struct zip_t *zip = zip_open("foo.zip", 0, 'r');
int i, n = zip_total_entries(zip);
int i, n = zip_entries_total(zip);
for (i = 0; i < n; ++i) {
zip_entry_openbyindex(zip, i);
{
Expand All @@ -172,6 +213,49 @@ for (i = 0; i < n; ++i) {
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);
}
```
* Deletes zip archive entries.
```c
char *entries[] = {"unused.txt", "remove.ini", "delete.me"};
struct zip_t *zip = zip_open("foo.zip", 0, 'd');
{
zip_entries_delete(zip, entries, 3);
}
zip_close(zip);
```

# Bindings
Compile zip library as a dynamic library.
```shell
Expand All @@ -181,7 +265,7 @@ $ cmake -DBUILD_SHARED_LIBS=true ..
$ make
```

### Go (cgo)
### [Go](https://golang.org) (cgo)
```go
package main

Expand Down Expand Up @@ -211,7 +295,7 @@ func main() {
}
```

### Rust (ffi)
### [Rust](https://www.rust-lang.org) (ffi)
```rust
extern crate libc;
use std::ffi::CString;
Expand All @@ -236,7 +320,7 @@ extern "C" {
}

fn main() {
let path = CString::new("/tmp/test.zip").unwrap();
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();
Expand All @@ -258,7 +342,7 @@ fn main() {
}
```

### Ruby (ffi)
### [Ruby](http://www.ruby-lang.org) (ffi)
Install _ffi_ gem.
```shell
$ gem install ffi
Expand Down Expand Up @@ -291,7 +375,7 @@ Zip.zip_entry_close(ptr)
Zip.zip_close(ptr)
```

### Python (cffi)
### [Python](https://www.python.org) (cffi)
Install _cffi_ package
```shell
$ pip install cffi
Expand Down Expand Up @@ -325,7 +409,36 @@ Zip.zip_entry_close(ptr)
Zip.zip_close(ptr)
```

### Ring
### [Never](https://never-lang.readthedocs.io/) (ffi)
```never
extern "libzip.so" func zip_open(zipname: string, level: int, mode: char) -> c_ptr
extern "libzip.so" func zip_close(zip: c_ptr) -> void
extern "libzip.so" func zip_entry_open(zip: c_ptr, entryname: string) -> int
extern "libzip.so" func zip_entry_close(zip: c_ptr) -> int
extern "libzip.so" func zip_entry_write(zip: c_ptr, buf: string, bufsize: int) -> int
extern "libzip.so" func zip_entry_fwrite(zip: c_ptr, filename: string) -> int
func main() -> int
{
let content = "Test content"
let zip = zip_open("/tmp/never.zip", 6, 'w');
zip_entry_open(zip, "test.file");
zip_entry_fwrite(zip, "/tmp/test.txt");
zip_entry_close(zip);
zip_entry_open(zip, "test.content");
zip_entry_write(zip, content, length(content));
zip_entry_close(zip);
zip_close(zip);
0
}
```

### [Ring](http://ring-lang.net)
The language comes with RingZip based on this library
```ring
load "ziplib.ring"
Expand All @@ -342,13 +455,15 @@ new Zip {
}
```

# Contribution Rules/Coding Standards
No need to throw away your coding style, just do your best to follow default clang-format style.
Apply `clang-format` to the source files before commit:
```sh
for file in $(git ls-files | \grep -E '\.(c|h)$' | \grep -v -- '#')
do
clang-format -i $file
done
```
# Check out more cool projects which use this library:
- [Filament](https://github.com/google/filament): Filament is a real-time physically based rendering engine for Android, iOS, Linux, macOS, Windows, and WebGL. It is designed to be as small as possible and as efficient as possible on Android.
- [Hermes JS Engine](https://github.com/facebook/hermes): Hermes is a JavaScript engine optimized for fast start-up of React Native apps on Android. It features ahead-of-time static optimization and compact bytecode.
- [Open Asset Import Library](https://github.com/assimp/assimp): A library to import and export various 3d-model-formats including scene-post-processing to generate missing render data.
- [PowerToys](https://github.com/microsoft/PowerToys): Set of utilities for power users to tune and streamline their Windows 10 experience for greater productivity.
- [The Ring Programming Language](https://ring-lang.github.io): Innovative and practical general-purpose multi-paradigm language.
- [The V Programming Language](https://github.com/vlang/v): Simple, fast, safe, compiled. For developing maintainable software.
- [TIC-80](https://github.com/nesbox/TIC-80): TIC-80 is a FREE and OPEN SOURCE fantasy computer for making, playing and sharing tiny games.
- [Urho3D](https://github.com/urho3d/Urho3D): Urho3D is a free lightweight, cross-platform 2D and 3D game engine implemented in C++ and released under the MIT license. Greatly inspired by OGRE and Horde3D.
- [Vcpkg](https://github.com/microsoft/vcpkg): Vcpkg helps you manage C and C++ libraries on Windows, Linux and MacOS.
- [and more...](https://grep.app/search?q=kuba--/zip)

Loading

0 comments on commit feeb89a

Please sign in to comment.