bpo-32728: Add compresslevel support for zipfile and LZMA#5534
Closed
bbayles wants to merge 11 commits intopython:masterfrom
Closed
bpo-32728: Add compresslevel support for zipfile and LZMA#5534bbayles wants to merge 11 commits intopython:masterfrom
bbayles wants to merge 11 commits intopython:masterfrom
Conversation
ZackerySpytz
reviewed
Aug 5, 2019
| # Write a ZIP archive with that LZMA compression preset and ensure | ||
| # that the property header above is written to that archive. | ||
| with io.BytesIO() as fp: | ||
| kwargs = {'compression': self.compression, 'compresslevel': 1} |
Contributor
There was a problem hiding this comment.
I don't think there's a need for this kwargs variable.
Contributor
Author
There was a problem hiding this comment.
Quite right. I put it there to avoid a long line.
|
|
||
| def __init__(self, preset=None): | ||
| self._comp = None | ||
| if (preset is not None) and (preset not in self._PRESET_OPTIONS_MAP): |
Contributor
There was a problem hiding this comment.
I don't think the parentheses here are needed.
Contributor
Author
There was a problem hiding this comment.
Indeed. My thought was that it helped with readability.
Co-Authored-By: Zackery Spytz <[email protected]>
Co-Authored-By: Zackery Spytz <[email protected]>
epicfaace
reviewed
Aug 27, 2019
| .. versionadded:: 3.8 | ||
| The *strict_timestamps* keyword-only argument | ||
|
|
||
| .. versionchanged:: 3.8 |
Contributor
There was a problem hiding this comment.
Suggested change
| .. versionchanged:: 3.8 | |
| .. versionchanged:: 3.9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR extends the
compresslevelsupport added tozipfile.ZipFilein #5385 to the LZMA compression method. CurrentlyZIP_DEFLATEDandZIP_BZ2can be used withcompresslevel, butZIP_LZMAcan't.zipfilecurrently defines a customLZMACompressorinstead of usinglzma.LZMACompressor.This is so the parameters needed for the "LZMA Properties Header" in the ZIP file can be be written. This custom compressor sets the LZMA
dict_sizeto 8 MiB, but leaves the other compression parameters (those determined by LZMA's presets) unspecified.Here I have added a dictionary that maps the presets (0 through 9, plus 0 through 9 OR-ed with
lzma.PRESET_EXTREME(see these docs) to their compression parameters. See here forwhat each compression parameter means. The values can be be mostly assembled from the
xzman page (example), but see alsoliblzma'slzma_lzma_presetfunction.Once we have the compression parameters for the selected preset, then we write the header such that the ZIP file can be decoded later. I've added a few comments about this in the code; the current process is a bit unclear if you don't have the ZIP spec memorized.
One can see from this script that the compression results are different when using the keyword (default behavior does not change).
I've also added a test that checks that the correct property header is written to the ZIP file; this we can do with LZMA much more easily than with zlib or bzip2.
It may be noted that
tarfilehas a way of setting an LZMA archive's preset with a preset keyword (instead of compresslevel), but I don't think this is worth bringing here - (a) that capability is currently undocumented intarfile; (b) Thezipfileandtarfilemodules already differ substantially (e.g.,write()vs.add()methods); (c) it seems a bit wrong to make people remember thatzlibandbz2use compresslevel butlzmauses preset in this module.https://bugs.python.org/issue32728