Skip to content

Instantly share code, notes, and snippets.

@yashbhutoria
Last active November 25, 2019 06:13
Show Gist options
  • Save yashbhutoria/40a68fe13023923f01733cc2e3f03759 to your computer and use it in GitHub Desktop.
Save yashbhutoria/40a68fe13023923f01733cc2e3f03759 to your computer and use it in GitHub Desktop.
Extract 7z to bytes in memory using libarchive. In your case, you might be pulling it from a cloud storage and don't wan't to write in your secondary memory.
import libarchive.public #Not Secure?
# Reading the archived 7z file as bytes
with open('csvarchive.7z','rb') as f:
archive_bytes = f.read()
# The function that you might want to use
def archive_bytes_to_file_bytes(archive_bytes):
global entry_var
with libarchive.public.memory_reader(archive_bytes) as reader:
for entry in reader:
entry_var = entry
entry_bytes = b'' # Do you have a better way ?
for block in entry.get_blocks():
entry_bytes += block
yield entry_bytes
# If the files are text, this will print them
for file_bytes in archive_bytes_to_file_bytes(archive_bytes):
print(file_bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment