Delete Files in a ZIP Archive in Python

ZIP archives are commonly used to compress and store multiple files and folders. While creating and extracting ZIP archives is a common task, sometimes you may need to delete specific files or folders from an existing ZIP archive. Python offers various libraries to work with ZIP archives, and one powerful library is Aspose.ZIP for Python. So let’s learn how to delete files from a ZIP archive in Python.

Python Library to Delete Files in ZIP Archives

Aspose.ZIP for Python is a feature-rich library for working with ZIP archives. It provides simple and efficient ways to create, manipulate, and extract files and folders from ZIP archives in Python. We will use this library to delete files from ZIP archives.

You can either download the library or install it from PyPI.

> pip install aspose-zip

Delete Files in a ZIP Archive in Python

You can access and delete the files from a ZIP archive in two ways: by name or by index. Deleting a file by name is useful when you exactly know the name of the file entry in the ZIP archive. Or you need to delete all files containing a particular word or phrase in their names. On the other hand, deleting files by index is used when you exactly know the order of the files in the ZIP archive.

So let’s have a look at both ways in detail.

Delete File from a ZIP by Name

For deleting files by name, you first need to filter the files matching/containing the word/phrase you provide. Once you have the list of files to be deleted, you can remove them from the ZIP archive one by one. The following are the steps to delete files from a ZIP archive in Python.

  • First, load the ZIP file using Archive class.
  • Then, loop through the files in the ZIP archive.
  • Filter the required files/folders and add them to a list.
  • Delete each entry in the filtered list using Archive.delete_entry(ArchiveEntry) method.
  • Finally, save the updated ZIP archive using Archive.save() method.

The following code snippet shows how to delete files in a ZIP archive in Python.

Remove Files in a ZIP by Index

The following are the steps to remove a file from the ZIP archive using its index.

  • First, load the ZIP file using Archive class.
  • Then, delete required entry from ZIP using Archive.delete_entry(index) method.
  • Finally, save the updated ZIP archive using Archive.save() method.

The below code snippet shows how to delete a file from a ZIP archive by index.

Python ZIP Library - Get a Free License

You can remove files inside ZIP archives without evaluation limitations by getting a free temporary license.

Learning Resources

Learn more about our Python ZIP library using the resources given below:

Online ZIP Tools

We have also developed some useful online tools to work with ZIP archives. These tools are absolutely free and are powered by Aspose.ZIP for Python.

Conclusion

In this blog post, we’ve explored how to delete files from a ZIP archive in Python. We have covered how to delete files from ZIP archives by name or by index. Whether you need to remove unwanted files from an existing ZIP archive or perform other ZIP-related tasks, Aspose.ZIP for Python has you covered. Simply install the library and enrich your Python application with advanced ZIP archiving features.

See Also