python-libarchive-c
目前使用python 2.7、3.4、3.5和3.6进行测试。
用于 libarchive
的Python接口。 它使用标准的模块动态加载和访问C库。
python-libarchive-c
可能无法与过时的libarchive版本(如MacOS中包含的版本)正常工作。
在这种情况下,可以安装最新版本的 libarchive
(例如,使用 brew
在MacOS上安装 libarchive
)。
sudo apt install python3-libarchive-c
import os
if os.path.exists("xx_test.tar.gz"): os.remove('xx_test.tar.gz')
with open('xx_text.txt', 'w') as fo:
fo.write('Hello')
!ls xx*
xx_11.jpg xx_111113.zip xx_112233.txt xx_123.txt xx_555.jpg xx_chapter.ipynb xx_dst.txt xx_hello.txt xx_hello2.txt xx_new2.zip xx_new_demo.txt xx_out.zip xx_sec01_Python-中-pathlib-模块的使用_jta0b3.ok xx_sec02_文件与路径处理_jt9aeb.ok xx_sec03_如何使用-os-模块遍历目录树_jta82c.ok xx_sec04_Python-中-shutil-模块的使用_jtf947.ok xx_sec04_Python-中shutil模块的使用_jtf947.ok xx_sec05_Python-中的-glob-模块_jtf359.ok xx_sec06_利用-zipfile-模块压缩文件_jt41fd.ok xx_sec07_项目将带有美国风格日期的文件改名为欧洲风格日期_jt6f4b.ok xx_sec08_项目文件夹备份到-ZIP-文件_jtce86.ok xx_sec10_计算机文件系统.ok xx_sec12_案例删除垃圾文件_file.ok xx_sec13_Python-调用操作系统工具.ok xx_sec13_Python调用操作系统工具.ok xx_sec14_Python-处理文件与文件夹.ok xx_sec15_如何使用-shutil-模块处理文件.ok xx_sec16_压缩文件处理.ok xx_sec17_案例压缩文件夹中某类型文件_files.ok xx_t.tar xx_test.tar xx_test.zip xx_text.txt xx_your.tar xx_archive: demo.json new.json new_demo.txt xx_channel1: xx_your.tar xx_dir: subdir xx_new: hello1.txt hello2.txt xx_out: hello1.txt hello2.txt new
import libarchive
with libarchive.file_writer('xx_test.tar.gz', 'ustar', 'gzip') as archive:
archive.add_files('xx_text.txt')
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[4], line 1 ----> 1 import libarchive 2 with libarchive.file_writer('xx_test.tar.gz', 'ustar', 'gzip') as archive: 3 archive.add_files('xx_text.txt') ModuleNotFoundError: No module named 'libarchive'
!ls xx*
if os.path.exists('xx_text.txt'): os.remove('xx_text.txt')
libarchive.extract_file('xx_test.tar.gz')
!ls xx*
with libarchive.file_reader('xx_test.tar.gz') as reader:
for e in reader:
# (The entry evaluates to a filename.)
# print("> %s" % (e))
print(e.name)
Python-libarchive-c
模块为 Python 提供了对 libarchive
库的访问接口,支持多种压缩格式(如 ZIP、TAR、RAR 等)的读写操作。
通过 libarchive
模块可创建、提取和检查归档文件,使用 file_reader
读取归档内容,
file_writer
创建新归档,memory_reader
和 memory_writer
处理内存中的数据流。
该模块支持流式处理大文件,并保留文件权限和元数据,适合需要跨平台压缩解压的场景。
安装时需确保系统已安装 libarchive
库(如 apt-get install libarchive-dev
),
然后通过 pip install libarchive-c
安装 Python 绑定,使用时注意异常处理和资源释放以确保稳定性。