Add Files to ZIP
Users often face the challenge of adding new files to existing ZIP archives. The typical process involves extracting the current archive, adding new files, and compressing everything into a new ZIP file. This method is cumbersome and time-consuming, especially when dealing with multiple ZIP archives.
A more efficient solution is to utilize a library that allows for programmatic manipulation of ZIP files. IronZip excels in this regard with its powerful Add method, enabling users to integrate new files seamlessly without requiring extraction. In the following code example, we will demonstrate the intuitive nature of the Add method, showcasing how effortlessly it allows you to append new files to existing ZIP archives with just a few lines of code.
Add Files to ZIP with C#
- using IronZip
- using (var archive = IronZipArchive.FromFile("existing.zip"))
- archive.Add("./assets/image3.png");
- archive.Add("./assets/image4.png");
- archive.SaveAs("result.zip");
Accessing Existing ZIP to Add Files
We first import the namespace IronZip. Afterward, we initialize a new IronZipArchive class and using the FromFile method input one parameter: the path to the ZIP you're trying to access. Do note that the operation will fail if the path is incorrect.
Adding Files
After accessing the ZIP file, we can then add files to it. We call the Add method to add new files to the existing ZIP. The method takes in one parameter, which is the path to the file you wish to add; similar to above, the operation will fail if the path provided is incorrect. Within the example, we provide two file path images,image3.png and image4.png, and add them to the existing ZIP with Add.
Aside from adding PNG, IronZIP also supports various file types compatible with the Add method. Users can also add other popular image formats such as jpg, svg, and even multi-frame images such as tiff and gif. Other files such as text files, documents (PDF, DOCX, XLSX), and audio files (MP3 and WAV) are also supported; even adding ZIP archives within a ZIP archive is supported, making it versatile. For a complete list of files you can add, please refer here.
Exporting the ZIP
Finally, after adding the files to the existing ZIP, we call SaveAs and provide a new name for the ZIP archive with the newly added files.
Discover how to create, read, and extract ZIP files with IronZIP in our comprehensive tutorial.