Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | 7z ANSI-C Decoder 4.23 |
michael@0 | 2 | ---------------------- |
michael@0 | 3 | |
michael@0 | 4 | 7z ANSI-C Decoder 4.23 Copyright (C) 1999-2005 Igor Pavlov |
michael@0 | 5 | |
michael@0 | 6 | 7z ANSI-C provides 7z/LZMA decoding. |
michael@0 | 7 | 7z ANSI-C version is simplified version ported from C++ code. |
michael@0 | 8 | |
michael@0 | 9 | LZMA is default and general compression method of 7z format |
michael@0 | 10 | in 7-Zip compression program (www.7-zip.org). LZMA provides high |
michael@0 | 11 | compression ratio and very fast decompression. |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | LICENSE |
michael@0 | 15 | ------- |
michael@0 | 16 | |
michael@0 | 17 | Read lzma.txt for information about license. |
michael@0 | 18 | |
michael@0 | 19 | |
michael@0 | 20 | Files |
michael@0 | 21 | --------------------- |
michael@0 | 22 | |
michael@0 | 23 | 7zAlloc.* - Allocate and Free |
michael@0 | 24 | 7zBuffer.* - Buffer structure |
michael@0 | 25 | 7zCrc.* - CRC32 code |
michael@0 | 26 | 7zDecode.* - Low level memory->memory decoding |
michael@0 | 27 | 7zExtract.* - High level stream->memory decoding |
michael@0 | 28 | 7zHeader.* - .7z format constants |
michael@0 | 29 | 7zIn.* - .7z archive opening |
michael@0 | 30 | 7zItem.* - .7z structures |
michael@0 | 31 | 7zMain.c - Test application |
michael@0 | 32 | 7zMethodID.* - MethodID structure |
michael@0 | 33 | 7zTypes.h - Base types and constants |
michael@0 | 34 | |
michael@0 | 35 | |
michael@0 | 36 | How To Use |
michael@0 | 37 | ---------- |
michael@0 | 38 | |
michael@0 | 39 | You must download 7-Zip program from www.7-zip.org. |
michael@0 | 40 | |
michael@0 | 41 | You can create .7z archive with 7z.exe or 7za.exe: |
michael@0 | 42 | |
michael@0 | 43 | 7za.exe a archive.7z *.htm -r -mx -m0fb=255 -mf=off |
michael@0 | 44 | |
michael@0 | 45 | If you have big number of files in archive, and you need fast extracting, |
michael@0 | 46 | you can use partly-solid archives: |
michael@0 | 47 | |
michael@0 | 48 | 7za.exe a archive.7z *.htm -ms=512K -r -mx -m0fb=255 -m0d=512K -mf=off |
michael@0 | 49 | |
michael@0 | 50 | In that example 7-Zip will use 512KB solid blocks. So it needs to decompress only |
michael@0 | 51 | 512KB for extracting one file from such archive. |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | Limitations of current version of 7z ANSI-C Decoder |
michael@0 | 55 | --------------------------------------------------- |
michael@0 | 56 | |
michael@0 | 57 | - It reads only "FileName", "Size", and "CRC" information for each file in archive. |
michael@0 | 58 | - It supports only LZMA and Copy (no compression) methods. |
michael@0 | 59 | - It converts original UTF-16 Unicode file names to UTF-8 Unicode file names. |
michael@0 | 60 | |
michael@0 | 61 | These limitations will be fixed in future versions. |
michael@0 | 62 | |
michael@0 | 63 | |
michael@0 | 64 | Using 7z ANSI-C Decoder Test application: |
michael@0 | 65 | ----------------------------------------- |
michael@0 | 66 | |
michael@0 | 67 | Usage: 7zDec <command> <archive_name> |
michael@0 | 68 | |
michael@0 | 69 | <Command>: |
michael@0 | 70 | e: Extract files from archive |
michael@0 | 71 | l: List contents of archive |
michael@0 | 72 | t: Test integrity of archive |
michael@0 | 73 | |
michael@0 | 74 | Example: |
michael@0 | 75 | |
michael@0 | 76 | 7zDec l archive.7z |
michael@0 | 77 | |
michael@0 | 78 | lists contents of archive.7z |
michael@0 | 79 | |
michael@0 | 80 | 7zDec e archive.7z |
michael@0 | 81 | |
michael@0 | 82 | extracts files from archive.7z to current folder. |
michael@0 | 83 | |
michael@0 | 84 | |
michael@0 | 85 | How to use .7z Decoder |
michael@0 | 86 | ---------------------- |
michael@0 | 87 | |
michael@0 | 88 | .7z Decoder can be compiled in one of two modes: |
michael@0 | 89 | |
michael@0 | 90 | 1) Default mode. In that mode 7z Decoder will read full compressed |
michael@0 | 91 | block to RAM before decompressing. |
michael@0 | 92 | |
michael@0 | 93 | 2) Mode with defined _LZMA_IN_CB. In that mode 7z Decoder can read |
michael@0 | 94 | compressed block by parts. And you can specify desired buffer size. |
michael@0 | 95 | So memory requirements can be reduced. But decompressing speed will |
michael@0 | 96 | be 5-10% lower and code size is slightly larger. |
michael@0 | 97 | |
michael@0 | 98 | |
michael@0 | 99 | Memory allocation |
michael@0 | 100 | ~~~~~~~~~~~~~~~~~ |
michael@0 | 101 | |
michael@0 | 102 | 7z Decoder uses two memory pools: |
michael@0 | 103 | 1) Temporary pool |
michael@0 | 104 | 2) Main pool |
michael@0 | 105 | Such scheme can allow you to avoid fragmentation of allocated blocks. |
michael@0 | 106 | |
michael@0 | 107 | Steps for using 7z decoder |
michael@0 | 108 | -------------------------- |
michael@0 | 109 | |
michael@0 | 110 | Use code at 7zMain.c as example. |
michael@0 | 111 | |
michael@0 | 112 | 1) Declare variables: |
michael@0 | 113 | inStream /* implements ISzInStream interface */ |
michael@0 | 114 | CArchiveDatabaseEx db; /* 7z archive database structure */ |
michael@0 | 115 | ISzAlloc allocImp; /* memory functions for main pool */ |
michael@0 | 116 | ISzAlloc allocTempImp; /* memory functions for temporary pool */ |
michael@0 | 117 | |
michael@0 | 118 | 2) call InitCrcTable(); function to initialize CRC structures. |
michael@0 | 119 | |
michael@0 | 120 | 3) call SzArDbExInit(&db); function to initialize db structures. |
michael@0 | 121 | |
michael@0 | 122 | 4) call SzArchiveOpen(inStream, &db, &allocMain, &allocTemp) to open archive |
michael@0 | 123 | |
michael@0 | 124 | This function opens archive "inStream" and reads headers to "db". |
michael@0 | 125 | All items in "db" will be allocated with "allocMain" functions. |
michael@0 | 126 | SzArchiveOpen function allocates and frees temporary structures by "allocTemp" functions. |
michael@0 | 127 | |
michael@0 | 128 | 5) List items or Extract items |
michael@0 | 129 | |
michael@0 | 130 | Listing code: |
michael@0 | 131 | ~~~~~~~~~~~~~ |
michael@0 | 132 | { |
michael@0 | 133 | UInt32 i; |
michael@0 | 134 | for (i = 0; i < db.Database.NumFiles; i++) |
michael@0 | 135 | { |
michael@0 | 136 | CFileItem *f = db.Database.Files + i; |
michael@0 | 137 | printf("%10d %s\n", (int)f->Size, f->Name); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | Extracting code: |
michael@0 | 142 | ~~~~~~~~~~~~~~~~ |
michael@0 | 143 | |
michael@0 | 144 | SZ_RESULT SzExtract( |
michael@0 | 145 | ISzInStream *inStream, |
michael@0 | 146 | CArchiveDatabaseEx *db, |
michael@0 | 147 | UInt32 fileIndex, /* index of file */ |
michael@0 | 148 | UInt32 *blockIndex, /* index of solid block */ |
michael@0 | 149 | Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */ |
michael@0 | 150 | size_t *outBufferSize, /* buffer size for output buffer */ |
michael@0 | 151 | size_t *offset, /* offset of stream for required file in *outBuffer */ |
michael@0 | 152 | size_t *outSizeProcessed, /* size of file in *outBuffer */ |
michael@0 | 153 | ISzAlloc *allocMain, |
michael@0 | 154 | ISzAlloc *allocTemp); |
michael@0 | 155 | |
michael@0 | 156 | If you need to decompress more than one file, you can send these values from previous call: |
michael@0 | 157 | blockIndex, |
michael@0 | 158 | outBuffer, |
michael@0 | 159 | outBufferSize, |
michael@0 | 160 | You can consider "outBuffer" as cache of solid block. If your archive is solid, |
michael@0 | 161 | it will increase decompression speed. |
michael@0 | 162 | |
michael@0 | 163 | After decompressing you must free "outBuffer": |
michael@0 | 164 | allocImp.Free(outBuffer); |
michael@0 | 165 | |
michael@0 | 166 | 6) call SzArDbExFree(&db, allocImp.Free) to free allocated items in "db". |
michael@0 | 167 | |
michael@0 | 168 | |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | Memory requirements for .7z decoding |
michael@0 | 172 | ------------------------------------ |
michael@0 | 173 | |
michael@0 | 174 | Memory usage for Archive opening: |
michael@0 | 175 | - Temporary pool: |
michael@0 | 176 | - Memory for compressed .7z headers (if _LZMA_IN_CB is not defined) |
michael@0 | 177 | - Memory for uncompressed .7z headers |
michael@0 | 178 | - some other temporary blocks |
michael@0 | 179 | - Main pool: |
michael@0 | 180 | - Memory for database: |
michael@0 | 181 | Estimated size of one file structures in solid archive: |
michael@0 | 182 | - Size (4 or 8 Bytes) |
michael@0 | 183 | - CRC32 (4 bytes) |
michael@0 | 184 | - Some file information (4 bytes) |
michael@0 | 185 | - File Name (variable length) + pointer + allocation structures |
michael@0 | 186 | |
michael@0 | 187 | Memory usage for archive Decompressing: |
michael@0 | 188 | - Temporary pool: |
michael@0 | 189 | - Memory for compressed solid block (if _LZMA_IN_CB is not defined) |
michael@0 | 190 | - Memory for LZMA decompressing structures |
michael@0 | 191 | - Main pool: |
michael@0 | 192 | - Memory for decompressed solid block |
michael@0 | 193 | |
michael@0 | 194 | |
michael@0 | 195 | If _LZMA_IN_CB is defined, 7z Decoder will not allocate memory for |
michael@0 | 196 | compressed blocks. Instead of this, you must allocate buffer with desired |
michael@0 | 197 | size before calling 7z Decoder. Use 7zMain.c as example. |
michael@0 | 198 | |
michael@0 | 199 | |
michael@0 | 200 | |
michael@0 | 201 | EXIT codes |
michael@0 | 202 | ----------- |
michael@0 | 203 | |
michael@0 | 204 | 7z Decoder functions can return one of the following codes: |
michael@0 | 205 | |
michael@0 | 206 | #define SZ_OK (0) |
michael@0 | 207 | #define SZE_DATA_ERROR (1) |
michael@0 | 208 | #define SZE_OUTOFMEMORY (2) |
michael@0 | 209 | #define SZE_CRC_ERROR (3) |
michael@0 | 210 | |
michael@0 | 211 | #define SZE_NOTIMPL (4) |
michael@0 | 212 | #define SZE_FAIL (5) |
michael@0 | 213 | |
michael@0 | 214 | #define SZE_ARCHIVE_ERROR (6) |
michael@0 | 215 | |
michael@0 | 216 | |
michael@0 | 217 | |
michael@0 | 218 | LZMA Defines |
michael@0 | 219 | ------------ |
michael@0 | 220 | |
michael@0 | 221 | _LZMA_IN_CB - Use special callback mode for input stream to reduce memory requirements |
michael@0 | 222 | |
michael@0 | 223 | _SZ_FILE_SIZE_64 - define it if you need support for files larger than 4 GB |
michael@0 | 224 | _SZ_NO_INT_64 - define it if your compiler doesn't support long long int |
michael@0 | 225 | |
michael@0 | 226 | _LZMA_PROB32 - it can increase LZMA decompressing speed on some 32-bit CPUs. |
michael@0 | 227 | |
michael@0 | 228 | _SZ_ONE_DIRECTORY - define it if you want to locate all source files to one directory |
michael@0 | 229 | _SZ_ALLOC_DEBUG - define it if you want to debug alloc/free operations to stderr. |
michael@0 | 230 | |
michael@0 | 231 | |
michael@0 | 232 | --- |
michael@0 | 233 | |
michael@0 | 234 | http://www.7-zip.org |
michael@0 | 235 | http://www.7-zip.org/support.html |