Skip to content

Fix the issue #404 #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions py7zr/archiveinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ def _read(self, fp: BinaryIO):
self._read_start_pos(buffer)
else:
raise Bad7zFile("invalid type %r" % prop) # pragma: no-cover
# re-calculate self.emptyfiles for next append
if numfiles > 0 and len(self.emptyfiles) == 0:
for f in self.files:
self.emptyfiles.append(f.get("emptystream"))

def _read_name(self, buffer: BinaryIO) -> None:
for f in self.files:
Expand Down
3 changes: 3 additions & 0 deletions py7zr/py7zr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,9 @@ def write(self, fp: BinaryIO, f, assym, folder):
def writestr(self, fp: BinaryIO, f, folder):
compressor = folder.get_compressor()
insize, foutsize, crc = compressor.compress(f.data(), fp)
if insize == 0:
fp.seek(-1 * foutsize, 1) # rewind
return 0, None
return self._after_write(insize, foutsize, crc)

def flush_archive(self, fp, folder):
Expand Down
Binary file added tests/data/github_404.zip
Binary file not shown.
66 changes: 66 additions & 0 deletions tests/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shutil
import stat
import sys
import zipfile
from datetime import datetime

import pytest
Expand Down Expand Up @@ -1123,3 +1124,68 @@ def test_compress_append_archive_w_zerofile(tmp_path):
#
with py7zr.SevenZipFile(target, "r") as arc:
arc.extractall(path=tmp_path / "tgt")


@pytest.mark.basic
def test_compress_file_append_dir(tmp_path):
srcpath = tmp_path.joinpath("src")
srcpath.mkdir()
with zipfile.ZipFile(pathlib.Path(testdata_path).joinpath("github_404.zip")) as zipsrc:
zipsrc.extractall(path=srcpath)
target = tmp_path.joinpath("target.7z")
archive = py7zr.SevenZipFile(target, "w")
archive.write(srcpath.joinpath("a.xlsx"), arcname="a.xlsx")
archive.close()
#
archive = py7zr.SevenZipFile(target, "a")
archive.set_encoded_header_mode(False)
archive.writeall(srcpath.joinpath("22"), arcname="22")
assert len(archive.header.main_streams.unpackinfo.folders) == 2
assert archive.header.main_streams.unpackinfo.numfolders == 2
#
assert len(archive.header.files_info.files) == 8
assert archive.header.files_info.files[0].get("filename") == "a.xlsx"
assert archive.header.files_info.files[0].get("digest") == 0x83ef96ba
assert archive.header.files_info.files[1].get("filename") == "22"
assert archive.header.files_info.files[2].get("filename") == "22/a.xlsx"
assert archive.header.files_info.files[2].get("digest") == 0x6d452c78
assert archive.header.files_info.files[3].get("filename") == "22/extra.csv"
assert archive.header.files_info.files[3].get("digest") == 0xfb91a7eb
assert archive.header.files_info.files[4].get("filename") == "22/purchase_notice.csv"
assert archive.header.files_info.files[4].get("digest") == 0x18991f62
assert archive.header.files_info.files[5].get("filename") == "22/res11.csv"
assert archive.header.files_info.files[5].get("digest") == 0x4719daea
assert archive.header.files_info.files[6].get("filename") == "22/res122.csv"
assert archive.header.files_info.files[6].get("digest") == 0x72f7c99f
assert archive.header.files_info.files[7].get("filename") == "22/res_end.csv"
assert archive.header.files_info.files[7].get("digest") == 0xaa8b30d0
#
assert len(archive.header.files_info.emptyfiles) == 8
assert not archive.header.files_info.emptyfiles[0]
assert archive.header.files_info.emptyfiles[1]
assert not archive.header.files_info.emptyfiles[2]
#
assert len(archive.files.files_list) == 8
assert archive.files.files_list[0].get("filename") == "a.xlsx"
assert archive.files.files_list[0].get("uncompressed") == 10059
assert archive.files.files_list[1].get("filename") == "22"
assert archive.files.files_list[1].get("emptystream") is True
assert archive.files.files_list[2].get("filename") == "22/a.xlsx"
assert archive.files.files_list[2].get("uncompressed") == 45298
assert archive.files.files_list[3].get("filename") == "22/extra.csv"
assert archive.files.files_list[3].get("uncompressed") == 45298
assert archive.files.files_list[4].get("filename") == "22/purchase_notice.csv"
assert archive.files.files_list[4].get("uncompressed") == 45298
assert archive.files.files_list[5].get("filename") == "22/res11.csv"
assert archive.files.files_list[5].get("uncompressed") == 10838
assert archive.files.files_list[6].get("filename") == "22/res122.csv"
assert archive.files.files_list[6].get("uncompressed") == 236458
assert archive.files.files_list[7].get("filename") == "22/res_end.csv"
assert archive.files.files_list[7].get("uncompressed") == 216681
archive.close()
#
p7zip_test(target)
libarchive_extract(target, tmp_path.joinpath("tgt2"))
#
with py7zr.SevenZipFile(target, "r") as arc:
arc.extractall(path=tmp_path / "tgt")