Xceed .NET Libraries Documentation
Zipping items in memory

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Basic Concepts > Zip and streaming capabilities > Zipping > Zipping items in memory

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:

To write a data directly in a zip file, the following steps must be performed:

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.
VB.NET Copy Code
Imports Xceed.Zip
Imports Xceed.FileSystem
Imports System.IO

' 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)

stream.Write(byteData, 0, byteData.Length)
stream.Close()
C# Copy Code
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" ) );

ZippedFile file = ( ZippedFile )zipFile.GetFile( "file.txt" );

if( !file.Exists )
    file.Create();
 
string data = "This is the data which will be added to the zip file";

// Convert the data to a byte array.
byte[] byteData = System.Text.Encoding.Default.GetBytes( data );

// Write the information to the ZippedFile object
using( Stream stream = file.OpenWrite( true ) )
{
    stream.Write( byteData, 0, byteData.Length );
}

Things you should consider

The main questions you should ask yourself when copying items located in memory to a zip file are:

All zip files will automatically be created in the Zip64 zip file format if the limitations of the regular Zip format are reached.