python / cpython

The Python programming language
https://www.python.org
Other
63.72k stars 30.53k forks source link

Supporting lzma compression in zip files #58574

Closed serhiy-storchaka closed 12 years ago

serhiy-storchaka commented 12 years ago
BPO 14366
Nosy @loewis, @pitrou, @merwok, @serhiy-storchaka
Files
  • bzip2_and_lzma_in_zip.patch
  • sample-bzip2.zip: Sample zip file with bzip2 compression. Can be unpacked by Info-Unzip, 7-Zip, WinZip, etc.
  • sample-lzma.zip: Sample zip file with lzma compression. Can be unpacked by 7-Zip, WinZip, etc. Future Info-Unzip 6.1 will unpack it.
  • bzip2_and_lzma_in_zip_3.patch: Unified patch for bzip2 and lzma compression
  • lzma_in_zip_2.patch
  • lzma_in_zip_3.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = created_at = labels = ['type-feature', 'library'] title = 'Supporting lzma compression in zip files' updated_at = user = 'https://github.com/serhiy-storchaka' ``` bugs.python.org fields: ```python activity = actor = 'serhiy.storchaka' assignee = 'none' closed = True closed_date = closer = 'loewis' components = ['Library (Lib)'] creation = creator = 'serhiy.storchaka' dependencies = [] files = ['24931', '24932', '24933', '25007', '25488', '25528'] hgrepos = [] issue_num = 14366 keywords = ['patch'] message_count = 22.0 messages = ['156288', '156291', '156295', '156297', '156340', '156370', '156395', '156644', '156674', '156676', '159745', '159746', '159806', '159810', '160052', '160109', '160163', '160341', '160373', '160509', '160510', '160514'] nosy_count = 7.0 nosy_names = ['loewis', 'alanmcintyre', 'pitrou', 'nadeem.vawda', 'eric.araujo', 'python-dev', 'serhiy.storchaka'] pr_nums = [] priority = 'normal' resolution = 'fixed' stage = None status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue14366' versions = ['Python 3.3'] ```

    serhiy-storchaka commented 12 years ago

    ZIP files specification supports new compression algorithms since 2006. Since bzip2 and lzma now contained in Python standart library, it would be nice to add support for these methods in zipfile. This will allow to process more foreign zip files and create more compact distributives.

    The proposed patch adds two new methods ZIP_BZIP2 and ZIP_LZMA, which are automatically detecting when unpacking and that can be used for packing.

    serhiy-storchaka commented 12 years ago

    This is not completed patch yet, without tests and documentation. I would like to receive feedback before the end of polishing. It might be worth transfer a portion of code in _lzmamodule.c for better use of capacity of LZMA API (used in zip format somewhat differs from LZMA_ALONE).

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 12 years ago

    ISTM that the LZMA support differs from the specification, see

    http://www.pkware.com/documents/casestudies/APPNOTE.TXT

    In particular, there appears to be no support for the EOS marker, which should be emitted when compressing.

    Changing the LZMA module is fine as long as it a) happens before the release of 3.3, and b) is truly justified by the ZIP spec

    I also recommend to split this issue into two: bzip support and lzma support. Adding bzip support might be easier.

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 12 years ago

    I also think that create_version and extract_version need to be adjusted.

    Since LZMA is version 6.3, we need to check for any features that might be in a zip file of extract version 6.3 or lower that we do not support (such as PPMd+ compression, strong encryption, etc.). In general, if we claim to support version x.y, we need to recognize that a feature is used that is supported for x1.y1 (x1.y1 \<= x.y) even if we don't support the feature.

    serhiy-storchaka commented 12 years ago

    Thank you, Martin, for review and advices.

    Lzma in zip format: 2-bytes version (LZMA SDK version, it has not relations with version of XZ Utils used by lzma module), 2-bytes properties size (I have not seen a value other than 5), N-bytes (N=5) property data, and raw compressed data (LZMA_RAW).

    Lzma file format (LZMA_ALONE): 5-bytes property data, 8-bytes uncompressed size (~0 if unknown), and raw compressed data (LZMA_RAW).

    7-Zip ignores version and supports only 5-bytes property data. Because the LZMA1 codec is declared obsolete, it is highly unlikely for new versions with properties size != 5. Nevertheless, it would be wise to create a lzma module functions for parsing the bytes to the codec properties and for dumping the codec properties to the bytes (this is functions lzma_lzma_props_encode() and lzma_lzma_props_decode() in liblzma). It is not necessary but desirable. I see no other reasonable choice but to hardcode some arbitrary version in the compressing and to ignore it in the decompressing.

    This EOS marker is only helpful for stream zip-files when the size of the compressed data is not known beforehand and it is not possible to specify the following (see lzma-file-format.txt in liblzma docs). But that's must be another issue, the current implementation of the zipfile module does not work with non-seekable files (I hope to work on it later).

    I also recommend to split this issue into two: bzip support and lzma support.

    Assuredly. I will create a new issue for bzip2, but what do I do with lzma? Do I need to rename this issue or create a new one? Does the lzma patch include the bzip2 patch, because the latter will contain the code necessary to support all codecs? Or should defer any work with lzma until the bzip2 support will commited?

    I think we should add the ability to register new codecs. Support for PPMd, jpeg and WavPack is unlikely to emerge in the Python in the foreseeable future, but users of third-party libraries (such as PIL), will use the new codecs as needed.

    I also think that create_version and extract_version need to be adjusted.

    Agree. Should we raise an exception when using new compressor if allowZip64 == False? Or set allowZip64 = True, if we explicitly use the new compressor?

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 12 years ago

    For EOS, please re-read the specification. If you then still think it is not needed, read it again :-) The documentation in liblzma is irrelevant, only the PKWARE specification matters. Take particular notice of the phrase "implementers should include the EOS marker whenever possible"

    For bzip: propose a patch that does just the bzip stuff, and any infrastructure changes needed for it. Having the LZMA patch depend on this is fine.

    Re: extensible compressors. I don't think that's needed. There is only a finite set, and if somebody wants to support some compression method, they should submit a patch.

    Re: allowZip64. This depends on whether you create or extract. Not using a feature on creation is fine - we don't *have* to use all supported features. On extraction, if a feature is used and we support it, it should get used regardless of any configuration (note: I didn't check what allowZip64 currently does).

    Re: 7zip. What it does is irrelevant. The ZIP format is defined by PKWARE, so if you want to look at a reference implementation, use theirs. Else use the spec.

    serhiy-storchaka commented 12 years ago

    Issue bpo-14371: Add support for bzip2 compression to the zipfile module.

    252699e1-f617-4a8b-9fb0-ae90945f6292 commented 12 years ago

    I plan on doing a review of the patch, but it might be a week or two before I have time to do it.

    Regarding changes to lzma; exactly what is being proposed? If it's just additional functions for encoding and decoding of filter specs, then I'm fine with that (and it's not *necessary* to get it into 3.3, though it would still be nice).

    serhiy-storchaka commented 12 years ago

    For EOS, please re-read the specification.

    Well, nothing prevents the setting of this bit. Lzma raw compressor already appends EOS.

    serhiy-storchaka commented 12 years ago

    Regarding changes to lzma; exactly what is being proposed?

    Yes, it's just additional functions for encoding and decoding of filter specs. But this is not necessary, the current implementation uses own functions (need the support of only LZMA1 format, which is unlikely to change).

    serhiy-storchaka commented 12 years ago

    Here is the patch which adds support for lzma in zipfile. Later I'll move lzma_props_encode and lzma_props_decode to lzma module.

    serhiy-storchaka commented 12 years ago

    Note that the supporting of bzip2 increases the time of testing test_zipfile in 1.5x, and lzma -- 2x (total 3x). May be not all tests are needed?

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 12 years ago

    I'm not sure where the test run time actually comes from. It's certainly desirable to reduce the test run time, as long as all test purposes are preserved. It's not necessary to do redundant tests, e.g. if some test isn't about compression, there is no point in running it with all compressors (again, I'm not sure whether this actually happens).

    pitrou commented 12 years ago

    Note that the supporting of bzip2 increases the time of testing test_zipfile in 1.5x, and lzma -- 2x (total 3x). May be not all tests are needed?

    I think it's ok. Some of our tests run quite longer than that; and better to be exhaustive than fast (even though exhaustive *and* fast is better, of course).

    252699e1-f617-4a8b-9fb0-ae90945f6292 commented 12 years ago

    I've put together a patch for the lzma module adding functions for encoding and decoding filter properties (see bpo-14736). It's a bit bulky, though, so I'd like to get a review before committing it.

    252699e1-f617-4a8b-9fb0-ae90945f6292 commented 12 years ago

    I've committed my patch as changeset 9118ef2b651a, adding functions encode_filter_properties and decode_filter_properties to the lzma module.

    serhiy-storchaka commented 12 years ago

    The patch updated to use functions encode_filter_properties and decode_filter_properties from the lzma module. Thank you, Nadeem Vawda.

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 12 years ago

    Serhiy, just to be sure we communicated well: I'm not asking that all the missing features for a 6.3 level zip library must be implemented. Instead, we need to detect whether an unsupported feature is used, and raise an exception reporting what the feature is that is unsupported. This is necessary as we otherwise may return incorrect data.

    serhiy-storchaka commented 12 years ago

    I understand you, Martin. The time it took me to read the specification and understand where should be the checks in the module. The patch updated. The list of compression methods extended (in the future it can be used for detailed output in the printdir()), flag of strict encryption is checked directly, encrypted or compressed central directory just will not be found (BadZipFile will be raised).

    1762cc99-3127-4a62-9baf-30c3d0f51ef7 commented 12 years ago

    New changeset fccdcd83708a by Martin v. Löwis in branch 'default': Issue bpo-14366: Support lzma compression in zip files. http://hg.python.org/cpython/rev/fccdcd83708a

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 12 years ago

    Thanks for the patch!

    serhiy-storchaka commented 12 years ago

    Thank you, Martin. Both of these patches basically your merit.

    Maybe take a look also at the small patches to bpo-14315 and bpo-10376?