pull/3058/head
kimkulling 2020-03-09 10:51:26 +01:00
parent 339f9387a7
commit bb3db0ebaf
6 changed files with 81 additions and 17 deletions

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0)
project(zip project(zip
LANGUAGES C LANGUAGES C
VERSION "0.1.15") VERSION "0.1.18")
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
option(CMAKE_DISABLE_TESTING "Disable test creation" OFF) option(CMAKE_DISABLE_TESTING "Disable test creation" OFF)
@ -16,10 +16,6 @@ elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang") "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -Werror -pedantic") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -Werror -pedantic")
if(ENABLE_COVERAGE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()
endif (MSVC) endif (MSVC)
# zip # zip
@ -35,7 +31,7 @@ if (NOT CMAKE_DISABLE_TESTING)
enable_testing() enable_testing()
add_subdirectory(test) add_subdirectory(test)
find_package(Sanitizers) find_package(Sanitizers)
add_sanitizers(${PROJECT_NAME} ${test_out} ${test_miniz_out}) add_sanitizers(${PROJECT_NAME} ${test_out})
endif() endif()
#### ####

View File

@ -1,10 +1,8 @@
### A portable (OSX/Linux/Windows), simple zip library written in C ### 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. 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.
[![Windows](https://ci.appveyor.com/api/projects/status/bph8dr3jacgmjv32/branch/master?svg=true&label=windows)](https://ci.appveyor.com/project/kuba--/zip) [![Build](https://github.com/kuba--/zip/workflows/build/badge.svg)](https://github.com/kuba--/zip/actions?query=workflow%3Abuild)
[![Linux](https://travis-ci.org/kuba--/zip.svg?branch=master&label=linux%2fosx)](https://travis-ci.org/kuba--/zip)
[![Version](https://badge.fury.io/gh/kuba--%2Fzip.svg)](https://github.com/kuba--/zip/releases) [![Version](https://badge.fury.io/gh/kuba--%2Fzip.svg)](https://github.com/kuba--/zip/releases)
[![Codecov](https://codecov.io/gh/kuba--/zip/branch/master/graph/badge.svg)](https://codecov.io/gh/kuba--/zip)
# The Idea # The Idea
@ -213,6 +211,53 @@ func main() {
} }
``` ```
### Rust (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/test.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 (ffi) ### Ruby (ffi)
Install _ffi_ gem. Install _ffi_ gem.
```shell ```shell

View File

@ -222,6 +222,20 @@ void zip_close(struct zip_t *zip) {
} }
} }
int zip_is64(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (!zip->archive.m_pState) {
// zip state is not initialized
return -1;
}
return (int)zip->archive.m_pState->m_zip64;
}
int zip_entry_open(struct zip_t *zip, const char *entryname) { int zip_entry_open(struct zip_t *zip, const char *entryname) {
size_t entrylen = 0; size_t entrylen = 0;
mz_zip_archive *pzip = NULL; mz_zip_archive *pzip = NULL;
@ -794,7 +808,8 @@ int zip_create(const char *zipname, const char *filenames[], size_t len) {
if (MZ_FILE_STAT(name, &file_stat) != 0) { if (MZ_FILE_STAT(name, &file_stat) != 0) {
// problem getting information - check errno // problem getting information - check errno
return -1; status = -1;
break;
} }
if ((file_stat.st_mode & 0200) == 0) { if ((file_stat.st_mode & 0200) == 0) {

View File

@ -21,7 +21,7 @@ extern "C" {
#if !defined(_SSIZE_T_DEFINED) && !defined(_SSIZE_T_DEFINED_) && \ #if !defined(_SSIZE_T_DEFINED) && !defined(_SSIZE_T_DEFINED_) && \
!defined(__DEFINED_ssize_t) && !defined(__ssize_t_defined) && \ !defined(__DEFINED_ssize_t) && !defined(__ssize_t_defined) && \
!defined(_SSIZE_T) && !defined(_SSIZE_T_) !defined(_SSIZE_T) && !defined(_SSIZE_T_) && !defined(_SSIZE_T_DECLARED)
// 64-bit Windows is the only mainstream platform // 64-bit Windows is the only mainstream platform
// where sizeof(long) != sizeof(void*) // where sizeof(long) != sizeof(void*)
@ -37,6 +37,7 @@ typedef long ssize_t; /* byte count or error */
#define __ssize_t_defined #define __ssize_t_defined
#define _SSIZE_T #define _SSIZE_T
#define _SSIZE_T_ #define _SSIZE_T_
#define _SSIZE_T_DECLARED
#endif #endif
@ -90,6 +91,16 @@ extern struct zip_t *zip_open(const char *zipname, int level, char mode);
*/ */
extern void zip_close(struct zip_t *zip); extern void zip_close(struct zip_t *zip);
/**
* Determines if the archive has a zip64 end of central directory headers.
*
* @param zip zip archive handler.
*
* @return the return code - 1 (true), 0 (false), negative number (< 0) on
* error.
*/
extern int zip_is64(struct zip_t *zip);
/** /**
* Opens an entry by name in the zip archive. * Opens an entry by name in the zip archive.
* *

View File

@ -2,15 +2,10 @@ cmake_minimum_required(VERSION 2.8)
# test # test
set(test_out test.out) set(test_out test.out)
set(test_miniz_out test_miniz.out)
add_executable(${test_out} test.c) add_executable(${test_out} test.c)
target_link_libraries(${test_out} zip) target_link_libraries(${test_out} zip)
add_executable(${test_miniz_out} test_miniz.c)
target_link_libraries(${test_miniz_out} zip)
add_test(NAME ${test_out} COMMAND ${test_out}) add_test(NAME ${test_out} COMMAND ${test_out})
add_test(NAME ${test_miniz_out} COMMAND ${test_miniz_out})
set(test_out ${test_out} PARENT_SCOPE) set(test_out ${test_out} PARENT_SCOPE)
set(test_miniz_out ${test_miniz_out} PARENT_SCOPE)

View File

@ -47,7 +47,7 @@ static void test_write(void) {
assert(CRC32DATA1 == zip_entry_crc32(zip)); assert(CRC32DATA1 == zip_entry_crc32(zip));
++total_entries; ++total_entries;
assert(0 == zip_entry_close(zip)); assert(0 == zip_entry_close(zip));
assert(0 == zip_is64(zip));
zip_close(zip); zip_close(zip);
} }
@ -92,6 +92,7 @@ static void test_read(void) {
size_t buftmp; size_t buftmp;
struct zip_t *zip = zip_open(ZIPNAME, 0, 'r'); struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
assert(zip != NULL); assert(zip != NULL);
assert(0 == zip_is64(zip));
assert(0 == zip_entry_open(zip, "test\\test-1.txt")); assert(0 == zip_entry_open(zip, "test\\test-1.txt"));
assert(strlen(TESTDATA1) == zip_entry_size(zip)); assert(strlen(TESTDATA1) == zip_entry_size(zip));
@ -310,6 +311,7 @@ static void test_fwrite(void) {
assert(0 == zip_entry_open(zip, WFILE)); assert(0 == zip_entry_open(zip, WFILE));
assert(0 == zip_entry_fwrite(zip, WFILE)); assert(0 == zip_entry_fwrite(zip, WFILE));
assert(0 == zip_entry_close(zip)); assert(0 == zip_entry_close(zip));
assert(0 == zip_is64(zip));
zip_close(zip); zip_close(zip);
remove(WFILE); remove(WFILE);