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.
Basic steps
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.
Demonstration
This example demonstrates how to copy a file located in memory to a zip file.
VB.NET
Copy Code
Imports Xceed.Zip Imports Xceed.FileSystem
' Note: Pathnames must be modified for code snippets to work under the .NET Compact Framework.
Dim file As New MemoryFile("RAM_DRIVE", "file.txt")
If Not file.Exists Then file.Create() End If
Dim zip As New ZipArchive(New DiskFile("d:\dump\test.zip") ) file.CopyTo(zip, True)
C#
Copy Code
using Xceed.Zip; using Xceed.FileSystem;
// Note: Pathnames must be modified for code snippets to work under the .NET Compact Framework. MemoryFile file = new MemoryFile( "RAM_DRIVE", "file.txt" );
if( !file.Exists ) file.Create();
ZipArchive zip = new ZipArchive( new DiskFile( @"d:\dump\test.zip" ) ); file.CopyTo( zip, true );
This next example demonstrates how to write data directly in a zip file.
' Note: Pathnames must be modified for code snippets to work under the .NET Compact Framework. Dim zipFile As New ZipArchive(New DiskFile("d:\dump\test.zip")) Dim file As ZippedFile = CType(zipFile.GetFile("file.txt"), ZippedFile)
If Not file.Exists Then file.Create() End If
Dim data As String = "This is the data which will be added to the zip file"
' Convert the data to a byte array. Dim byteData() As Byte = System.Text.Encoding.Default.GetBytes(data)
' Write the information to the ZippedFile object Dim stream As Stream = file.OpenWrite(True)
using Xceed.Zip; using Xceed.FileSystem; using System.IO;
// If your trial period has expired, you must purchase a registered license key, // uncomment the next line of code, and insert your registered license key. // For more information, jump to the How the 45-day trial works and the // How to license the component topics. //Xceed.Zip.Licenser.LicenseKey = "ZINXX-XXXXX-XXXXX-XXXX"; // Note: Pathnames must be modified for code snippets to work under the .NET Compact Framework.
ZipArchive zipFile = new ZipArchive( new DiskFile( @"d:\dump\test.zip" ) );