LibArchiveFS
Description:
- Compressed archives as a filesystem
Resources:
- [GitHub] https://github.com/fsspec/filesystem_spec/pull/460/files
- [Docs] https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.implementations.libarchive.LibArchiveFileSystem
Environment variables
Section titled “Environment variables”This notebook reads its settings from environment variables. Set them before starting Jupyter:
S3_BUCKET: name of an S3 bucket you can access
Before you run Examples 1-4, upload two archives of your own to that bucket: any 7z archive as ganymede-demo/archive.7z and any zip archive as ganymede-demo/example.zip. Example 5 downloads a public archive over HTTPS.
Example 1
Section titled “Example 1”List the contents of a remote 7z file on s3
import os
from fsspec import filesystem
# Open the 7z archive in your S3 bucket as a filesystemlibarchive_fs = filesystem("libarchive", fo=f"s3://{os.environ['S3_BUCKET']}/ganymede-demo/archive.7z")
# See what's in it: the path of every file in the archivearchive_files = libarchive_fs.find("")archive_filesExample 2
Section titled “Example 2”Download one file in a remote 7z archive on s3 to local
from pathlib import Path
tmp = Path("/tmp/ganymede_demo")tmp.mkdir(parents=True, exist_ok=True)
# Download the first file in the archive from s3 to localmember = archive_files[0]local_member = tmp / Path(member).namelibarchive_fs.get(rpath=member, lpath=str(local_member))Example 3
Section titled “Example 3”See info about an object in the archive and open the local copy
# Get info about the object inside the archiveprint(libarchive_fs.info(path=member))
# Open the local copy and read its first byteswith open(local_member, "rb") as f: print(f.read(64))Example 4
Section titled “Example 4”Download a remote zip file from S3 to local and see what’s in it
import os
from fsspec import filesystemfrom pathlib import Path
libarchive_fs_s3 = filesystem("libarchive", fo=f"s3://{os.environ['S3_BUCKET']}/ganymede-demo/example.zip")print(libarchive_fs_s3.ls("/", detail=False))
Path("/tmp/ganymede_demo").mkdir(parents=True, exist_ok=True)
libarchive_fs_s3.get( rpath="/", lpath="/tmp/ganymede_demo/unarchived", recursive=True)
local_fs = filesystem("local")local_fs.ls("/tmp/ganymede_demo/unarchived", detail=False)Example 5
Section titled “Example 5”Download a remote sqlite database locally
from pathlib import Pathfrom fsspec import filesystem
# Create a new dirPath("/tmp/ganymede_demo/sqlite").mkdir(parents=True, exist_ok=True)
# Get the zip archive from URLlibarchive_fs = filesystem("libarchive", fo="https://www.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip")
# Download it locallylibarchive_fs.get(lpath="/tmp/ganymede_demo/sqlite/chinook.db", rpath="chinook.db")
local_fs = filesystem("local")local_fs.ls("/tmp/ganymede_demo/sqlite/chinook.db", detail=False)ganymede is made by Eugo