Overview
In some situations, it is necessary to selectively delete files from the archive. Removal can be performed based on a mask or any other criterion.Aspose.ZIP supports selective deletion from several formats.
Python Remove File From ZIP Archive
The delete_entry method is used to delete an entire entry from a ZIP archive. This operation is independent and does not require reprocessing the entire archive.
Entries of ZIP archive can be deleted using
DeleteEntry
pair of methods. Deleting an entry occurs without repacking, so other entries remain unchanged without being decompressed and recompressed.
with zp.Archive("archive.zip") as archive:
while len(archive.entries) > 1:
archive.delete_entry(archive.entries[0])
archive.save("last_entry.zip")
This code processes the existing ZIP archive "archive.zip". It successivelyremoves all entries except the last one, effectively creating a new archive named "last_entry.zip" that contains only the remaining file.
Remove File From TAR
The delete_entry method is also effective for removing entries from a TAR archive. This process operates independently, eliminating the need to reprocess the entire archive. By using the delete_entry method for TAR files, as shown in the example below, entries can be deleted seamlessly, ensuring that the remaining entries are not affected and do not undergo decompression or recompression.
with zp.tar.TarArchive("two_files.tar") as archive:
archive.delete_entry(0)
archive.save("single_file.tar")
If you want to delete entries from compressed tar (*.tar.gz), it is necessary to decompress it to plain *.tar.
CPIO Remove File From Archive
Entries can be deleted from CPIO similar to TAR.
with zp.cpio.CpioArchive("two_files.cpio") as archive:
archive.delete_entry(archive.entries[0]).save("single_file.cpio")
Remember, the delete_entry method returns the archive instance. This will help you to make your code more compact in some cases.
Delete From XAR Files
Xar archive also has single entry deletion method delete_entry.
In this sample all of the entries are removed but the last one.
with zp.xar.XarArchive("archive.xar") as archive:
while len(archive.entries) > 1:
archive.delete_entry(0)
archive.save("last.xar")
This example showcases isolated processing, meaning it modifies the archive without affecting the compressed state of remaining entries. The code iteratively deletes entries from "archive.xar" until just one remains. Finally, the resulting archive is saved as "last.xar".
Other Supported Aspose.ZIP for Python.Net API Features
With the Aspose.ZIP library in Python.Net, you can seamlessly handle various tasks related to ZIP file documents. Whether you need to convert formats, merge files, edit contents, or extract data, Aspose.ZIP provides comprehensive functionality to streamline your workflow and enhance productivity.
Support and Learning Resources
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Release Notes
- Why Aspose.Zip for Python.Net?
- Customers List
- Success Stories