This topic demonstrates how to add items in memory to a zip file using both the MemoryFile class and the OpenWrite method of the ZippedFile class.
To copy an item located in memory to a zip file using the MemoryFile class, the following steps must be performed:
Retrieve a reference to a file located in memory using the MemoryFile class. With Xceed Zip for .NET, a file is a file; it does not matter if it is located within a zip file, on disk or in memory.
If the file does not exist, create it.
Retrieve a reference to a new or existing zip file using the ZipArchive class.
Call the CopyTo method to copy the entire contents of the folder to the zip file.
To write a data directly in a zip file, the following steps must be performed:
Retrieve a reference to a new or existing zip file using the ZipArchive class.
Retrieve a reference to a new or existing file inside the zip file using the ZippedFile class.
Convert the data to write to the file within the zip file to a byte array.
Open a stream to the ZippedFile object using its OpenWrite method.
Write the data to the ZippedFile object.
This example demonstrates how to copy a file located in memory to a zip file.
VB.NET | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
|
VB.NET | Copy Code |
---|---|
Imports Xceed.Zip |
C# | Copy Code |
---|---|
using Xceed.Zip; |
The main questions you should ask yourself when copying items located in memory to a zip file are:
Do you want to copy a folder rather than a file? Create a MemoryFolder class rather than a MemoryFile class.
Do you want to filter (specify specific files and folders) the items that are to be added to the zip file? Use filters.
Do you want to display the status of the operation? See the Events topic.
Do you want to add items into a specific folder within the zip file? Create an instance of a ZippedFolder object rather than a ZipArchive object.
Do you only want to copy the contents of folder? Use the CopyFilesTo method.
Do you want to move items rather than copy them? Use the MoveTo and MoveFilesTo methods.
Do you want to change the location of the temporary folder. Set the ZipArchive's TempFolder or DefaultTempFolder property.
Do you only want to do basic zip file operations? Use the QuickZip class.
Do you want to modify the default extra headers that are stored to a zip file? Set the ZipArchive's DefaultExtraHeaders property.
All zip files will automatically be created in the Zip64 zip file format if the limitations of the regular Zip format are reached.