packlens 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. packlens-0.1.0/CONTRIBUTING.md +38 -0
  2. packlens-0.1.0/LICENSE +21 -0
  3. packlens-0.1.0/MANIFEST.in +3 -0
  4. packlens-0.1.0/PKG-INFO +327 -0
  5. packlens-0.1.0/README.md +294 -0
  6. packlens-0.1.0/pyproject.toml +55 -0
  7. packlens-0.1.0/setup.cfg +4 -0
  8. packlens-0.1.0/src/packlens/__init__.py +5 -0
  9. packlens-0.1.0/src/packlens/__main__.py +4 -0
  10. packlens-0.1.0/src/packlens/analysis/__init__.py +0 -0
  11. packlens-0.1.0/src/packlens/analysis/comparison.py +174 -0
  12. packlens-0.1.0/src/packlens/analysis/duplicates.py +119 -0
  13. packlens-0.1.0/src/packlens/analysis/inspection.py +109 -0
  14. packlens-0.1.0/src/packlens/analysis/integrity.py +156 -0
  15. packlens-0.1.0/src/packlens/analysis/nested.py +59 -0
  16. packlens-0.1.0/src/packlens/analysis/nested_scan.py +104 -0
  17. packlens-0.1.0/src/packlens/analysis/paths.py +270 -0
  18. packlens-0.1.0/src/packlens/analysis/tree.py +120 -0
  19. packlens-0.1.0/src/packlens/cli.py +370 -0
  20. packlens-0.1.0/src/packlens/core/__init__.py +0 -0
  21. packlens-0.1.0/src/packlens/core/archive.py +102 -0
  22. packlens-0.1.0/src/packlens/core/detection.py +127 -0
  23. packlens-0.1.0/src/packlens/core/errors.py +41 -0
  24. packlens-0.1.0/src/packlens/core/limits.py +47 -0
  25. packlens-0.1.0/src/packlens/core/models.py +175 -0
  26. packlens-0.1.0/src/packlens/formats/__init__.py +25 -0
  27. packlens-0.1.0/src/packlens/formats/base.py +246 -0
  28. packlens-0.1.0/src/packlens/formats/compressed.py +174 -0
  29. packlens-0.1.0/src/packlens/formats/tar.py +258 -0
  30. packlens-0.1.0/src/packlens/formats/zip.py +218 -0
  31. packlens-0.1.0/src/packlens/output/__init__.py +0 -0
  32. packlens-0.1.0/src/packlens/output/json.py +25 -0
  33. packlens-0.1.0/src/packlens/output/manifest.py +99 -0
  34. packlens-0.1.0/src/packlens/output/terminal.py +364 -0
  35. packlens-0.1.0/src/packlens/py.typed +0 -0
  36. packlens-0.1.0/src/packlens/utils/__init__.py +0 -0
  37. packlens-0.1.0/src/packlens/utils/formatting.py +96 -0
  38. packlens-0.1.0/src/packlens/utils/hashing.py +32 -0
  39. packlens-0.1.0/src/packlens.egg-info/PKG-INFO +327 -0
  40. packlens-0.1.0/src/packlens.egg-info/SOURCES.txt +58 -0
  41. packlens-0.1.0/src/packlens.egg-info/dependency_links.txt +1 -0
  42. packlens-0.1.0/src/packlens.egg-info/entry_points.txt +2 -0
  43. packlens-0.1.0/src/packlens.egg-info/requires.txt +5 -0
  44. packlens-0.1.0/src/packlens.egg-info/top_level.txt +1 -0
  45. packlens-0.1.0/tests/conftest.py +41 -0
  46. packlens-0.1.0/tests/helpers.py +134 -0
  47. packlens-0.1.0/tests/test_cli.py +290 -0
  48. packlens-0.1.0/tests/test_comparison.py +88 -0
  49. packlens-0.1.0/tests/test_detection.py +123 -0
  50. packlens-0.1.0/tests/test_duplicates.py +76 -0
  51. packlens-0.1.0/tests/test_formatting.py +60 -0
  52. packlens-0.1.0/tests/test_integrity.py +209 -0
  53. packlens-0.1.0/tests/test_nested.py +121 -0
  54. packlens-0.1.0/tests/test_paths.py +148 -0
  55. packlens-0.1.0/tests/test_properties.py +66 -0
  56. packlens-0.1.0/tests/test_robustness.py +98 -0
  57. packlens-0.1.0/tests/test_streams.py +82 -0
  58. packlens-0.1.0/tests/test_tar.py +159 -0
  59. packlens-0.1.0/tests/test_tree.py +65 -0
  60. packlens-0.1.0/tests/test_zip.py +213 -0
@@ -0,0 +1,38 @@
1
+ # Contributing to Packlens
2
+
3
+ Thanks for helping. A few ground rules keep the project reliable.
4
+
5
+ ## Priorities
6
+
7
+ 1. Correctness and security over feature count.
8
+ 2. Treat everything from an archive (names, metadata, contents) as untrusted.
9
+ 3. Standard library only at runtime. Propose any dependency in an issue first.
10
+ 4. Never extract, execute or modify anything; never make network requests.
11
+
12
+ ## Setup
13
+
14
+ ```bash
15
+ git clone https://github.com/Sam3360/packlens
16
+ cd packlens
17
+ python -m venv .venv && source .venv/bin/activate
18
+ pip install -e ".[dev]"
19
+ pytest
20
+ ```
21
+
22
+ ## Making changes
23
+
24
+ - Add or update tests with every behaviour change. Build fixtures in code (`tests/helpers.py`);
25
+ do not commit binary archives.
26
+ - If you fix a crash found on malformed input, add a regression test that reproduces it.
27
+ - Keep format-specific code inside `src/packlens/formats/`. A new format is a new
28
+ `ArchiveHandler` registered in `formats/__init__.py` plus detection in `core/detection.py`.
29
+ Do not advertise a format in the README until its implementation and tests are complete.
30
+ - Anything printed from archive-derived text must go through `escape_for_terminal`; anything put
31
+ in JSON must go through `json_safe`.
32
+ - Keep exit codes stable; update the README table if you must change one.
33
+ - Use type hints, avoid broad `except Exception`, and catch specific error types.
34
+
35
+ ## Reporting security problems
36
+
37
+ Please report vulnerabilities privately through GitHub's security advisories for this repository
38
+ rather than in a public issue.
packlens-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sam3360
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ include LICENSE README.md CONTRIBUTING.md
2
+ graft tests
3
+ global-exclude __pycache__ *.py[cod]
@@ -0,0 +1,327 @@
1
+ Metadata-Version: 2.4
2
+ Name: packlens
3
+ Version: 0.1.0
4
+ Summary: Inspect archives. Understand contents.
5
+ Author: Sam3360
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Sam3360/packlens
8
+ Project-URL: Source, https://github.com/Sam3360/packlens
9
+ Project-URL: Issues, https://github.com/Sam3360/packlens/issues
10
+ Keywords: archive,zip,tar,inspect,verify,security,cli
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: System :: Archiving
23
+ Classifier: Topic :: Utilities
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.10
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Provides-Extra: dev
29
+ Requires-Dist: pytest>=7; extra == "dev"
30
+ Requires-Dist: hypothesis>=6; extra == "dev"
31
+ Requires-Dist: build; extra == "dev"
32
+ Dynamic: license-file
33
+
34
+ # Packlens
35
+
36
+ Inspect archives. Understand contents.
37
+
38
+ Packlens is a command-line tool that shows what is inside an archive, checks its integrity,
39
+ compares it with another archive and points out anything unusual, without extracting a single
40
+ file to disk. It uses only the Python standard library.
41
+
42
+ ```console
43
+ $ packlens inspect backup.zip
44
+ Archive backup.zip
45
+ Format ZIP
46
+ Contents 6 files, 3 directories, 1 symlink
47
+ Uncompressed 7.5 KiB
48
+ Compressed 1.6 KiB (archive size on disk)
49
+ Ratio 4.71x
50
+ Integrity available: Per-entry CRC-32 checks (requires reading all entry data)
51
+ run `packlens verify backup.zip` to perform the checks
52
+
53
+ Findings: 2 warning(s), 1 note(s)
54
+ These are observations about the archive, not conclusions about intent.
55
+ warning path-traversal x1
56
+ ../outside.txt: path climbs out of the extraction directory ('..'); extracting naively could write outside it
57
+ warning symlink-escape x1
58
+ project/current: symbolic link to '/etc/passwd' points outside the extraction directory
59
+ note nested-archive x1
60
+ old-backup.tar.gz: file looks like a nested tar.gz archive
61
+ ```
62
+
63
+ ## Principles
64
+
65
+ - Inspection never requires extraction, and the source archive is opened read-only.
66
+ - Names, metadata and contents are untrusted input. Output is escaped so a hostile file name
67
+ cannot inject terminal escape sequences, and JSON output is always valid.
68
+ - Security findings are warnings about what *would* happen under naive extraction, never claims
69
+ about intent.
70
+ - Verification says exactly what it checked. A TAR file has no content checksums, so `verify`
71
+ reports "structure and readability only" for it rather than "verified".
72
+ - Entry data is streamed in chunks; memory use does not grow with entry size.
73
+
74
+ Packlens never executes anything from an archive, never makes network requests and needs no
75
+ elevated privileges.
76
+
77
+ ## Installation
78
+
79
+ ```bash
80
+ pip install packlens
81
+ ```
82
+
83
+ Requires Python 3.10 or newer. Packlens has no runtime dependencies. To install from a clone:
84
+
85
+ ```bash
86
+ git clone https://github.com/Sam3360/packlens
87
+ cd packlens
88
+ pip install -e ".[dev]"
89
+ ```
90
+
91
+ ## Quick start
92
+
93
+ ```bash
94
+ packlens inspect backup.zip # summary and findings
95
+ packlens tree backup.tar.gz --depth 2 # directory view
96
+ packlens verify backup.zip # integrity check
97
+ packlens compare old.zip new.zip --by hash # what changed
98
+ packlens manifest backup.zip --sha256 -o manifest.json
99
+ packlens duplicates backup.zip
100
+ packlens inspect backup.zip --nested # also look inside archives within it
101
+ packlens tree backup.zip --into old-backup.tar.gz # open a nested archive
102
+ ```
103
+
104
+ ## Supported formats
105
+
106
+ | Format | Detected by content | Notes |
107
+ | --- | --- | --- |
108
+ | ZIP | yes | including ZIP64 and zips with a prefix (self-extractors); names are reported exactly as stored, so a backslash in a name is visible on every platform |
109
+ | TAR | yes | ustar, GNU and pax headers |
110
+ | TAR.GZ / TGZ | yes | |
111
+ | TAR.BZ2 / TBZ2 | yes | |
112
+ | TAR.XZ / TXZ | yes | |
113
+ | GZIP, BZIP2, XZ | yes | exposed as one logical entry holding the decompressed payload |
114
+
115
+ Formats are detected from content; file names are only a hint for empty archives and damaged
116
+ compressed streams. 7z, RAR and Zstandard are recognised so the error message can say so, but
117
+ they are **not supported yet**. Handlers live in `src/packlens/formats/` behind a common
118
+ interface (`ArchiveHandler`), so adding one does not touch the core.
119
+
120
+ ## Commands
121
+
122
+ Global options (accepted before or after the command): `-q/--quiet`, `-v/--verbose`,
123
+ `--no-color`, `--version`, `--help`.
124
+
125
+ Options shared by every command that opens an archive:
126
+
127
+ - `--into ENTRY` opens an archive stored inside the archive. Repeat it to go deeper.
128
+ - Resource limits: `--max-entries`, `--max-total-bytes`, `--max-entry-size`,
129
+ `--max-nesting-depth`, `--max-nested-size` (see [Resource limits](#resource-limits)).
130
+
131
+ ### inspect
132
+
133
+ Shows the format, entry counts, sizes, compression ratio, metadata, whether integrity checks
134
+ exist, and findings. `--format json` for machine output, `--nested` to inspect archives found
135
+ inside, `--strict` to exit with code 4 when any warning-level finding is reported.
136
+
137
+ ### tree
138
+
139
+ Directory view with sizes. Directories are merged (an explicit `a/` entry and the implied parent
140
+ of `a/b.txt` appear once), `--depth N` limits the levels shown and summarises what is hidden,
141
+ `--ascii` forces plain line drawing, `--format json` gives a flat list of nodes. Paths that are
142
+ repeated or stored as both file and directory are marked. Very deep paths are handled
143
+ iteratively.
144
+
145
+ ### verify
146
+
147
+ Reads every entry and lets the format's own checks run:
148
+
149
+ | Format | What is checked |
150
+ | --- | --- |
151
+ | ZIP | CRC-32 of every entry, declared size, local header consistency |
152
+ | TAR | header checksums, member readability, declared sizes, end-of-archive marker |
153
+ | TAR + gzip/bzip2/xz | the above plus the compression stream's checksum |
154
+ | GZIP / BZIP2 / XZ | the stream checksum (xz streams created without a check are reported as incomplete) |
155
+
156
+ Each check is **passed**, **failed** or **unavailable** (for example an encrypted ZIP entry or an
157
+ unsupported ZIP compression method). The overall result is `passed`, `failed` or `incomplete`.
158
+
159
+ ```console
160
+ $ packlens verify backup.zip
161
+ Archive backup.zip (ZIP)
162
+ Checks Per-entry CRC-32 checks (requires reading all entry data)
163
+ Entries 10 total: 7 passed, 0 failed, 0 unavailable, 3 with no data to check
164
+ Read 7.5 KiB decompressed
165
+ Structure passed: all headers and the end of the archive are well-formed
166
+
167
+ PASSED: content checksums matched for every readable entry.
168
+ ```
169
+
170
+ Python's `tarfile` treats a corrupted header after the first member as the end of the archive.
171
+ Packlens inspects the block where reading stopped, so truncation and damage between members are
172
+ reported instead of silently ignored.
173
+
174
+ ### compare
175
+
176
+ ```console
177
+ $ packlens compare old.zip new.zip
178
+ Comparing old.zip -> new.zip (by size)
179
+
180
+ + added new.txt 1 B
181
+ - removed gone.txt 1 B
182
+ ~ changed a.txt (size: 5 -> 6 bytes)
183
+ ~ changed b.txt (content differs (CRC-32 mismatch))
184
+
185
+ 1 added, 1 removed, 2 changed, 2 path(s) in both
186
+ ```
187
+
188
+ `--by path` compares names only. `--by size` (default) compares names and sizes, and also CRC-32
189
+ when both archives are ZIPs. `--by hash` streams both archives and compares SHA-256 (only for
190
+ files whose sizes already match). A matching size is never reported as "identical": it is
191
+ counted separately as "same size, content not compared" unless `--by hash` proves otherwise (two
192
+ empty files are always identical). `--metadata` also reports mode and modification-time
193
+ differences (2 s tolerance; ZIP and TAR timestamps are not compared with each other because ZIP
194
+ timestamps carry no time zone).
195
+
196
+ ### manifest
197
+
198
+ Writes a JSON manifest: path, type, size, compressed size, modification time, compression
199
+ method, CRC-32, mode, link target, plus an optional SHA-256 (`--sha256`). The manifest also
200
+ contains a summary and the findings. It is written incrementally, so large archives do not
201
+ accumulate in memory. If the archive turns out to be damaged part-way, stdout still receives
202
+ valid JSON with `"complete": false` and an `"error"`; with `--output`, no file is written. The
203
+ output file is never allowed to be the archive being read.
204
+
205
+ ### duplicates
206
+
207
+ Groups files by size, hashes only candidates that share a size, and streams each file once.
208
+ Duplicate **content** (same SHA-256) and repeated **paths** (the same name stored more than once)
209
+ are reported separately. Empty files are excluded unless `--include-empty`.
210
+
211
+ ## Security analysis
212
+
213
+ `inspect` (and `manifest`) report these findings. They describe what would happen if the archive
214
+ were extracted naively and are not a malware verdict.
215
+
216
+ | Code | Meaning |
217
+ | --- | --- |
218
+ | `absolute-path`, `windows-drive-path` | path starts with `/`, `\` or a drive letter |
219
+ | `path-traversal` | `..` components that climb above the extraction root |
220
+ | `parent-reference` | `..` that stays inside the root |
221
+ | `backslash-separator` | backslash in a name (a separator on Windows) |
222
+ | `control-characters`, `bidi-control`, `invalid-encoding` | control characters, text-direction overrides, non-UTF-8 names |
223
+ | `deep-nesting` | very deep directory nesting |
224
+ | `duplicate-path`, `type-conflict`, `case-collision` | repeated paths, file/directory clashes, names differing only by case or Unicode form |
225
+ | `symlink`, `symlink-escape`, `hardlink`, `hardlink-escape` | links, and whether their target leaves the root |
226
+ | `special-file`, `special-permissions` | devices/FIFOs; setuid or setgid bits |
227
+ | `large-entry`, `high-compression-ratio`, `high-total-ratio`, `many-entries` | possible resource-exhaustion indicators |
228
+ | `encrypted-entry` | contents cannot be checked |
229
+ | `nested-archive` | a file that looks like an archive (note only) |
230
+
231
+ Nested archives are *not* inspected unless you pass `--nested` (or `--into`). They are read into
232
+ memory, never written to disk, and bounded by the limits below. They are found by name and by
233
+ content signature, so a renamed archive is still noticed.
234
+
235
+ Heuristic thresholds (ratio 100x, 1 GiB entries, 100,000 entries, 32 path levels) are fields on
236
+ `packlens.core.limits.Limits`.
237
+
238
+ ## Resource limits
239
+
240
+ Limits stop an operation with exit code 1 and a message naming the option to raise. Use `0` (or
241
+ `unlimited`) for no limit. Sizes accept `10MB` (decimal) and `10MiB` (binary).
242
+
243
+ | Option | Default | Meaning |
244
+ | --- | --- | --- |
245
+ | `--max-entries` | 1,000,000 | entries per archive (ZIP counts are checked before parsing) |
246
+ | `--max-total-bytes` | 10 GiB | decompressed bytes processed in one command, across nested archives |
247
+ | `--max-entry-size` | 4 GiB | decompressed bytes read from one entry |
248
+ | `--max-nesting-depth` | 3 | levels of nested archives to inspect |
249
+ | `--max-nested-size` | 128 MiB | largest nested archive held in memory |
250
+
251
+ A compressed TAR or a standalone stream has to be decompressed to be listed, so these limits also
252
+ protect `inspect` against decompression bombs. For a compressed stream that exceeds a limit,
253
+ `inspect` still works and reports the uncompressed size as unknown.
254
+
255
+ ## Exit codes
256
+
257
+ | Code | Meaning |
258
+ | --- | --- |
259
+ | 0 | success |
260
+ | 1 | operation failed: unreadable or unsupported input, damaged archive that could not be listed fully, resource limit reached |
261
+ | 2 | invalid arguments or usage |
262
+ | 3 | `verify` found a failed check |
263
+ | 4 | `inspect --strict` reported at least one warning-level finding |
264
+
265
+ `verify` exits 0 for an `incomplete` result (nothing failed, but some checks could not run); the
266
+ text and JSON output say so explicitly. `compare` exits 0 whether or not differences are found.
267
+
268
+ ## Output modes
269
+
270
+ - Text output is the default. `--format json` is available on every command except `manifest`,
271
+ which always emits JSON. JSON documents carry `schema_version` and `packlens_version`.
272
+ - `--quiet` suppresses progress and informational output: `inspect` prints one line, `verify`
273
+ prints nothing on success. Requested data (trees, manifests, comparisons) is still printed.
274
+ - `--verbose` shows more samples per finding and lists every entry during `verify`.
275
+ - Progress is shown on stderr only when it is a terminal.
276
+
277
+ ## Known limitations
278
+
279
+ - 7z, RAR and Zstandard are not supported. Multi-volume ZIPs are not supported.
280
+ - ZIP: encrypted entries and compression methods unsupported by Python can be listed but not
281
+ verified. Zstandard-compressed ZIP entries can only be verified on Python 3.14 or newer, where
282
+ the standard library can decode them; on older versions they are reported as "unavailable". Python's `zipfile` holds the whole entry table in memory: a
283
+ 100,000-entry ZIP peaked at about 130 MiB in our test, so very large archives need
284
+ proportionally more memory.
285
+ - Compressed TARs have no random access: listing needs one decompression pass and hashing needs
286
+ another.
287
+ - A damaged ZIP whose central directory is gone is reported as unreadable; no recovery by
288
+ scanning local headers is attempted.
289
+ - A TAR file only proves structure, never content integrity.
290
+ - Link-escape analysis resolves paths lexically; it does not know what exists on a target
291
+ filesystem.
292
+ - Tested on Linux, Windows and macOS through the CI workflow.
293
+
294
+ ## Development
295
+
296
+ ```bash
297
+ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
298
+ pip install -e ".[dev]"
299
+ pytest
300
+ ```
301
+
302
+ Test archives are built programmatically in `tests/helpers.py`; no binary fixtures are stored.
303
+ `tests/test_robustness.py` mutates every format with seeded random corruption and asserts that only
304
+ deliberate Packlens errors ever escape. `tests/test_properties.py` uses Hypothesis and is skipped
305
+ when it is not installed.
306
+
307
+ Layout:
308
+
309
+ ```text
310
+ src/packlens/
311
+ cli.py argument parsing, exit codes
312
+ core/ models, limits, format detection, opening archives
313
+ formats/ ArchiveHandler interface and the ZIP / TAR / stream handlers
314
+ analysis/ integrity, paths/metadata, duplicates, comparison, tree, nested
315
+ output/ terminal and JSON rendering, streaming manifest writer
316
+ utils/ hashing and formatting helpers
317
+ ```
318
+
319
+ ## Contributing
320
+
321
+ Issues and pull requests are welcome at <https://github.com/Sam3360/packlens>. Please keep to the
322
+ project's priorities: correctness and security before features, no new runtime dependencies without
323
+ a strong reason, and tests for every behaviour change. See [CONTRIBUTING.md](CONTRIBUTING.md).
324
+
325
+ ## License
326
+
327
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,294 @@
1
+ # Packlens
2
+
3
+ Inspect archives. Understand contents.
4
+
5
+ Packlens is a command-line tool that shows what is inside an archive, checks its integrity,
6
+ compares it with another archive and points out anything unusual, without extracting a single
7
+ file to disk. It uses only the Python standard library.
8
+
9
+ ```console
10
+ $ packlens inspect backup.zip
11
+ Archive backup.zip
12
+ Format ZIP
13
+ Contents 6 files, 3 directories, 1 symlink
14
+ Uncompressed 7.5 KiB
15
+ Compressed 1.6 KiB (archive size on disk)
16
+ Ratio 4.71x
17
+ Integrity available: Per-entry CRC-32 checks (requires reading all entry data)
18
+ run `packlens verify backup.zip` to perform the checks
19
+
20
+ Findings: 2 warning(s), 1 note(s)
21
+ These are observations about the archive, not conclusions about intent.
22
+ warning path-traversal x1
23
+ ../outside.txt: path climbs out of the extraction directory ('..'); extracting naively could write outside it
24
+ warning symlink-escape x1
25
+ project/current: symbolic link to '/etc/passwd' points outside the extraction directory
26
+ note nested-archive x1
27
+ old-backup.tar.gz: file looks like a nested tar.gz archive
28
+ ```
29
+
30
+ ## Principles
31
+
32
+ - Inspection never requires extraction, and the source archive is opened read-only.
33
+ - Names, metadata and contents are untrusted input. Output is escaped so a hostile file name
34
+ cannot inject terminal escape sequences, and JSON output is always valid.
35
+ - Security findings are warnings about what *would* happen under naive extraction, never claims
36
+ about intent.
37
+ - Verification says exactly what it checked. A TAR file has no content checksums, so `verify`
38
+ reports "structure and readability only" for it rather than "verified".
39
+ - Entry data is streamed in chunks; memory use does not grow with entry size.
40
+
41
+ Packlens never executes anything from an archive, never makes network requests and needs no
42
+ elevated privileges.
43
+
44
+ ## Installation
45
+
46
+ ```bash
47
+ pip install packlens
48
+ ```
49
+
50
+ Requires Python 3.10 or newer. Packlens has no runtime dependencies. To install from a clone:
51
+
52
+ ```bash
53
+ git clone https://github.com/Sam3360/packlens
54
+ cd packlens
55
+ pip install -e ".[dev]"
56
+ ```
57
+
58
+ ## Quick start
59
+
60
+ ```bash
61
+ packlens inspect backup.zip # summary and findings
62
+ packlens tree backup.tar.gz --depth 2 # directory view
63
+ packlens verify backup.zip # integrity check
64
+ packlens compare old.zip new.zip --by hash # what changed
65
+ packlens manifest backup.zip --sha256 -o manifest.json
66
+ packlens duplicates backup.zip
67
+ packlens inspect backup.zip --nested # also look inside archives within it
68
+ packlens tree backup.zip --into old-backup.tar.gz # open a nested archive
69
+ ```
70
+
71
+ ## Supported formats
72
+
73
+ | Format | Detected by content | Notes |
74
+ | --- | --- | --- |
75
+ | ZIP | yes | including ZIP64 and zips with a prefix (self-extractors); names are reported exactly as stored, so a backslash in a name is visible on every platform |
76
+ | TAR | yes | ustar, GNU and pax headers |
77
+ | TAR.GZ / TGZ | yes | |
78
+ | TAR.BZ2 / TBZ2 | yes | |
79
+ | TAR.XZ / TXZ | yes | |
80
+ | GZIP, BZIP2, XZ | yes | exposed as one logical entry holding the decompressed payload |
81
+
82
+ Formats are detected from content; file names are only a hint for empty archives and damaged
83
+ compressed streams. 7z, RAR and Zstandard are recognised so the error message can say so, but
84
+ they are **not supported yet**. Handlers live in `src/packlens/formats/` behind a common
85
+ interface (`ArchiveHandler`), so adding one does not touch the core.
86
+
87
+ ## Commands
88
+
89
+ Global options (accepted before or after the command): `-q/--quiet`, `-v/--verbose`,
90
+ `--no-color`, `--version`, `--help`.
91
+
92
+ Options shared by every command that opens an archive:
93
+
94
+ - `--into ENTRY` opens an archive stored inside the archive. Repeat it to go deeper.
95
+ - Resource limits: `--max-entries`, `--max-total-bytes`, `--max-entry-size`,
96
+ `--max-nesting-depth`, `--max-nested-size` (see [Resource limits](#resource-limits)).
97
+
98
+ ### inspect
99
+
100
+ Shows the format, entry counts, sizes, compression ratio, metadata, whether integrity checks
101
+ exist, and findings. `--format json` for machine output, `--nested` to inspect archives found
102
+ inside, `--strict` to exit with code 4 when any warning-level finding is reported.
103
+
104
+ ### tree
105
+
106
+ Directory view with sizes. Directories are merged (an explicit `a/` entry and the implied parent
107
+ of `a/b.txt` appear once), `--depth N` limits the levels shown and summarises what is hidden,
108
+ `--ascii` forces plain line drawing, `--format json` gives a flat list of nodes. Paths that are
109
+ repeated or stored as both file and directory are marked. Very deep paths are handled
110
+ iteratively.
111
+
112
+ ### verify
113
+
114
+ Reads every entry and lets the format's own checks run:
115
+
116
+ | Format | What is checked |
117
+ | --- | --- |
118
+ | ZIP | CRC-32 of every entry, declared size, local header consistency |
119
+ | TAR | header checksums, member readability, declared sizes, end-of-archive marker |
120
+ | TAR + gzip/bzip2/xz | the above plus the compression stream's checksum |
121
+ | GZIP / BZIP2 / XZ | the stream checksum (xz streams created without a check are reported as incomplete) |
122
+
123
+ Each check is **passed**, **failed** or **unavailable** (for example an encrypted ZIP entry or an
124
+ unsupported ZIP compression method). The overall result is `passed`, `failed` or `incomplete`.
125
+
126
+ ```console
127
+ $ packlens verify backup.zip
128
+ Archive backup.zip (ZIP)
129
+ Checks Per-entry CRC-32 checks (requires reading all entry data)
130
+ Entries 10 total: 7 passed, 0 failed, 0 unavailable, 3 with no data to check
131
+ Read 7.5 KiB decompressed
132
+ Structure passed: all headers and the end of the archive are well-formed
133
+
134
+ PASSED: content checksums matched for every readable entry.
135
+ ```
136
+
137
+ Python's `tarfile` treats a corrupted header after the first member as the end of the archive.
138
+ Packlens inspects the block where reading stopped, so truncation and damage between members are
139
+ reported instead of silently ignored.
140
+
141
+ ### compare
142
+
143
+ ```console
144
+ $ packlens compare old.zip new.zip
145
+ Comparing old.zip -> new.zip (by size)
146
+
147
+ + added new.txt 1 B
148
+ - removed gone.txt 1 B
149
+ ~ changed a.txt (size: 5 -> 6 bytes)
150
+ ~ changed b.txt (content differs (CRC-32 mismatch))
151
+
152
+ 1 added, 1 removed, 2 changed, 2 path(s) in both
153
+ ```
154
+
155
+ `--by path` compares names only. `--by size` (default) compares names and sizes, and also CRC-32
156
+ when both archives are ZIPs. `--by hash` streams both archives and compares SHA-256 (only for
157
+ files whose sizes already match). A matching size is never reported as "identical": it is
158
+ counted separately as "same size, content not compared" unless `--by hash` proves otherwise (two
159
+ empty files are always identical). `--metadata` also reports mode and modification-time
160
+ differences (2 s tolerance; ZIP and TAR timestamps are not compared with each other because ZIP
161
+ timestamps carry no time zone).
162
+
163
+ ### manifest
164
+
165
+ Writes a JSON manifest: path, type, size, compressed size, modification time, compression
166
+ method, CRC-32, mode, link target, plus an optional SHA-256 (`--sha256`). The manifest also
167
+ contains a summary and the findings. It is written incrementally, so large archives do not
168
+ accumulate in memory. If the archive turns out to be damaged part-way, stdout still receives
169
+ valid JSON with `"complete": false` and an `"error"`; with `--output`, no file is written. The
170
+ output file is never allowed to be the archive being read.
171
+
172
+ ### duplicates
173
+
174
+ Groups files by size, hashes only candidates that share a size, and streams each file once.
175
+ Duplicate **content** (same SHA-256) and repeated **paths** (the same name stored more than once)
176
+ are reported separately. Empty files are excluded unless `--include-empty`.
177
+
178
+ ## Security analysis
179
+
180
+ `inspect` (and `manifest`) report these findings. They describe what would happen if the archive
181
+ were extracted naively and are not a malware verdict.
182
+
183
+ | Code | Meaning |
184
+ | --- | --- |
185
+ | `absolute-path`, `windows-drive-path` | path starts with `/`, `\` or a drive letter |
186
+ | `path-traversal` | `..` components that climb above the extraction root |
187
+ | `parent-reference` | `..` that stays inside the root |
188
+ | `backslash-separator` | backslash in a name (a separator on Windows) |
189
+ | `control-characters`, `bidi-control`, `invalid-encoding` | control characters, text-direction overrides, non-UTF-8 names |
190
+ | `deep-nesting` | very deep directory nesting |
191
+ | `duplicate-path`, `type-conflict`, `case-collision` | repeated paths, file/directory clashes, names differing only by case or Unicode form |
192
+ | `symlink`, `symlink-escape`, `hardlink`, `hardlink-escape` | links, and whether their target leaves the root |
193
+ | `special-file`, `special-permissions` | devices/FIFOs; setuid or setgid bits |
194
+ | `large-entry`, `high-compression-ratio`, `high-total-ratio`, `many-entries` | possible resource-exhaustion indicators |
195
+ | `encrypted-entry` | contents cannot be checked |
196
+ | `nested-archive` | a file that looks like an archive (note only) |
197
+
198
+ Nested archives are *not* inspected unless you pass `--nested` (or `--into`). They are read into
199
+ memory, never written to disk, and bounded by the limits below. They are found by name and by
200
+ content signature, so a renamed archive is still noticed.
201
+
202
+ Heuristic thresholds (ratio 100x, 1 GiB entries, 100,000 entries, 32 path levels) are fields on
203
+ `packlens.core.limits.Limits`.
204
+
205
+ ## Resource limits
206
+
207
+ Limits stop an operation with exit code 1 and a message naming the option to raise. Use `0` (or
208
+ `unlimited`) for no limit. Sizes accept `10MB` (decimal) and `10MiB` (binary).
209
+
210
+ | Option | Default | Meaning |
211
+ | --- | --- | --- |
212
+ | `--max-entries` | 1,000,000 | entries per archive (ZIP counts are checked before parsing) |
213
+ | `--max-total-bytes` | 10 GiB | decompressed bytes processed in one command, across nested archives |
214
+ | `--max-entry-size` | 4 GiB | decompressed bytes read from one entry |
215
+ | `--max-nesting-depth` | 3 | levels of nested archives to inspect |
216
+ | `--max-nested-size` | 128 MiB | largest nested archive held in memory |
217
+
218
+ A compressed TAR or a standalone stream has to be decompressed to be listed, so these limits also
219
+ protect `inspect` against decompression bombs. For a compressed stream that exceeds a limit,
220
+ `inspect` still works and reports the uncompressed size as unknown.
221
+
222
+ ## Exit codes
223
+
224
+ | Code | Meaning |
225
+ | --- | --- |
226
+ | 0 | success |
227
+ | 1 | operation failed: unreadable or unsupported input, damaged archive that could not be listed fully, resource limit reached |
228
+ | 2 | invalid arguments or usage |
229
+ | 3 | `verify` found a failed check |
230
+ | 4 | `inspect --strict` reported at least one warning-level finding |
231
+
232
+ `verify` exits 0 for an `incomplete` result (nothing failed, but some checks could not run); the
233
+ text and JSON output say so explicitly. `compare` exits 0 whether or not differences are found.
234
+
235
+ ## Output modes
236
+
237
+ - Text output is the default. `--format json` is available on every command except `manifest`,
238
+ which always emits JSON. JSON documents carry `schema_version` and `packlens_version`.
239
+ - `--quiet` suppresses progress and informational output: `inspect` prints one line, `verify`
240
+ prints nothing on success. Requested data (trees, manifests, comparisons) is still printed.
241
+ - `--verbose` shows more samples per finding and lists every entry during `verify`.
242
+ - Progress is shown on stderr only when it is a terminal.
243
+
244
+ ## Known limitations
245
+
246
+ - 7z, RAR and Zstandard are not supported. Multi-volume ZIPs are not supported.
247
+ - ZIP: encrypted entries and compression methods unsupported by Python can be listed but not
248
+ verified. Zstandard-compressed ZIP entries can only be verified on Python 3.14 or newer, where
249
+ the standard library can decode them; on older versions they are reported as "unavailable". Python's `zipfile` holds the whole entry table in memory: a
250
+ 100,000-entry ZIP peaked at about 130 MiB in our test, so very large archives need
251
+ proportionally more memory.
252
+ - Compressed TARs have no random access: listing needs one decompression pass and hashing needs
253
+ another.
254
+ - A damaged ZIP whose central directory is gone is reported as unreadable; no recovery by
255
+ scanning local headers is attempted.
256
+ - A TAR file only proves structure, never content integrity.
257
+ - Link-escape analysis resolves paths lexically; it does not know what exists on a target
258
+ filesystem.
259
+ - Tested on Linux, Windows and macOS through the CI workflow.
260
+
261
+ ## Development
262
+
263
+ ```bash
264
+ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
265
+ pip install -e ".[dev]"
266
+ pytest
267
+ ```
268
+
269
+ Test archives are built programmatically in `tests/helpers.py`; no binary fixtures are stored.
270
+ `tests/test_robustness.py` mutates every format with seeded random corruption and asserts that only
271
+ deliberate Packlens errors ever escape. `tests/test_properties.py` uses Hypothesis and is skipped
272
+ when it is not installed.
273
+
274
+ Layout:
275
+
276
+ ```text
277
+ src/packlens/
278
+ cli.py argument parsing, exit codes
279
+ core/ models, limits, format detection, opening archives
280
+ formats/ ArchiveHandler interface and the ZIP / TAR / stream handlers
281
+ analysis/ integrity, paths/metadata, duplicates, comparison, tree, nested
282
+ output/ terminal and JSON rendering, streaming manifest writer
283
+ utils/ hashing and formatting helpers
284
+ ```
285
+
286
+ ## Contributing
287
+
288
+ Issues and pull requests are welcome at <https://github.com/Sam3360/packlens>. Please keep to the
289
+ project's priorities: correctness and security before features, no new runtime dependencies without
290
+ a strong reason, and tests for every behaviour change. See [CONTRIBUTING.md](CONTRIBUTING.md).
291
+
292
+ ## License
293
+
294
+ MIT. See [LICENSE](LICENSE).