Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_zip.c File Reference

Reading members out of a ZIP archive, to a file. More...

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <zlib.h>
#include "nilorea/n_log.h"
#include "nilorea/n_zip.h"
Include dependency graph for n_zip.c:

Go to the source code of this file.

Data Structures

struct  ZIP_SINK
 where unpacked bytes go: a file, or a buffer in memory More...

Macros

#define N_ZIP_CHUNK   262144
 the pieces a member is read and written in
#define N_ZIP_TAIL   (22 + 65535)
 how much of an archive's end is searched for its end record: the record and the longest comment it may carry

Functions

static unsigned le16 (const unsigned char *p)
 helper: a little endian 16 bit value
static uint32_t le32 (const unsigned char *p)
 helper: a little endian 32 bit value
int n_zip_extract (const char *path, const char *name, const char *out)
 write a member out to a file, checking its CRC-32
int n_zip_find (const char *path, const char *name, N_ZIP_MEMBER *member)
 find a member by its full name in an archive's central directory
int n_zip_find_ending (const char *path, const char *ending, char *name, size_t name_len)
 find the first member whose name ends with a given text, as a PyTorch checkpoint's "archive/data.pkl" whose folder name varies
int n_zip_read (const char *path, const char *name, size_t max_size, unsigned char **data, size_t *len)
 read a member into memory, checking its CRC-32
static int seek_to (FILE *f, uint64_t at)
 helper: seek to a 64 bit offset where the platform's fseek takes a long
static int sink_put (ZIP_SINK *sink, const unsigned char *data, size_t len)
 put unpacked bytes where they go
static int zip_scan (const char *path, const char *name, int ending, N_ZIP_MEMBER *member, char *found_name, size_t found_len)
 find a member by its whole name, or by how its name ends
static int zip_unpack (const char *path, const char *name, const N_ZIP_MEMBER *member, ZIP_SINK *sink)
 unpack a member into a sink, checking its size and CRC-32

Detailed Description

Reading members out of a ZIP archive, to a file.

Author
Castagnier Mickael
Version
1.0
Date
28/09/2026

Definition in file n_zip.c.


Data Structure Documentation

◆ ZIP_SINK

struct ZIP_SINK

where unpacked bytes go: a file, or a buffer in memory

Definition at line 162 of file n_zip.c.

Collaboration diagram for ZIP_SINK:
Data Fields
unsigned char * buffer the buffer, allocated to the member's size
FILE * file the file, or NULL
size_t size its size
size_t used how much of it is written

Macro Definition Documentation

◆ N_ZIP_CHUNK

#define N_ZIP_CHUNK   262144

the pieces a member is read and written in

Definition at line 42 of file n_zip.c.

Referenced by zip_unpack().

◆ N_ZIP_TAIL

#define N_ZIP_TAIL   (22 + 65535)

how much of an archive's end is searched for its end record: the record and the longest comment it may carry

Definition at line 40 of file n_zip.c.

Referenced by zip_scan().

Function Documentation

◆ le16()

unsigned le16 ( const unsigned char * p)
static

helper: a little endian 16 bit value

Definition at line 45 of file n_zip.c.

Referenced by zip_scan(), and zip_unpack().

Here is the caller graph for this function:

◆ le32()

uint32_t le32 ( const unsigned char * p)
static

helper: a little endian 32 bit value

Examples
ex_icc.c.

Definition at line 49 of file n_zip.c.

Referenced by make_tiff(), make_webp(), zip_scan(), and zip_unpack().

Here is the caller graph for this function:

◆ seek_to()

int seek_to ( FILE * f,
uint64_t at )
static

helper: seek to a 64 bit offset where the platform's fseek takes a long

Definition at line 54 of file n_zip.c.

Referenced by zip_scan(), and zip_unpack().

Here is the caller graph for this function:

◆ sink_put()

int sink_put ( ZIP_SINK * sink,
const unsigned char * data,
size_t len )
static

put unpacked bytes where they go

Parameters
sinkthe destination
datathe bytes
lenhow many
Returns
0, or -1 when the file refused them or the buffer is full

Definition at line 178 of file n_zip.c.

References ZIP_SINK::buffer, ZIP_SINK::file, ZIP_SINK::size, and ZIP_SINK::used.

Referenced by zip_unpack().

Here is the caller graph for this function:

◆ zip_scan()

int zip_scan ( const char * path,
const char * name,
int ending,
N_ZIP_MEMBER * member,
char * found_name,
size_t found_len )
static

find a member by its whole name, or by how its name ends

Parameters
paththe archive
namethe name, or its ending
endingnon-zero to match the ending only
memberreceives what it is, may be NULL
found_namereceives the whole name when matching an ending, may be NULL
found_lensize of found_name
Returns
0 when found, -1 otherwise

Definition at line 67 of file n_zip.c.

References N_ZIP_MEMBER::compressed, N_ZIP_MEMBER::crc, le16(), le32(), LOG_ERR, N_ZIP_MEMBER::method, n_log, N_ZIP_TAIL, N_ZIP_MEMBER::offset, seek_to(), and N_ZIP_MEMBER::size.

Referenced by n_zip_find(), and n_zip_find_ending().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ zip_unpack()

int zip_unpack ( const char * path,
const char * name,
const N_ZIP_MEMBER * member,
ZIP_SINK * sink )
static

unpack a member into a sink, checking its size and CRC-32

Parameters
paththe archive
namethe member
memberwhat n_zip_find() said of it
sinkwhere the bytes go
Returns
0, or -1

Definition at line 192 of file n_zip.c.

References N_ZIP_MEMBER::compressed, N_ZIP_MEMBER::crc, done, le16(), le32(), LOG_ERR, N_ZIP_MEMBER::method, n_log, N_ZIP_CHUNK, N_ZIP_MEMBER::offset, seek_to(), sink_put(), and N_ZIP_MEMBER::size.

Referenced by n_zip_extract(), and n_zip_read().

Here is the call graph for this function:
Here is the caller graph for this function: