Xceed .NET Libraries Documentation
Creating a zip file in memory

Welcome to Xceed Data Manipulation Compoents for .NET and .NET Standard > Basic Concepts > Zip and streaming capabilities > Zipping > Creating a zip file in memory

This topic demonstrates how to create a zip file in memory.

Basic steps

To create a zip file in memory, the following steps must be performed:

  • Retrieve a reference to a folder whose files will be added to the zip file using either the DiskFolder, ZippedFolder, ZipArchive, MemoryFolder or IsolatedFolder classes. With Xceed Zip for .NET, a folder is a folder; it does not matter if it is located within a zip file, on disk or in memory. 

  • Retrieve a reference to a new or existing zip file using the ZipArchive class. Because we want the zip file to reside in memory, we will use a MemoryFile in the constructor of the ZipArchive class. 

  • Call the CopyFilesTo method to copy the entire contents of the folder to the zip file.

Demonstration

This example demonstrates how to copy the contents of a folder located on disk to a zip file located in memory.

public static void ZipIntoMemory()
{
  // Select a file that will be our zip file
  AbstractFile zipFile = new MemoryFile( "RAM_Disk", "CopyItemsToZip2.zip" );

  /* The component doesn't have distinct add and update operations.
     If you want any existing zip file to be overwritten, you need to delete the
     zip file before starting to perform any operation. */

  // If the zip file already exists
  if( zipFile.Exists )
    // Delete it
    zipFile.Delete();

  // Create a logical zip archive around the zip file
  ZipArchive zip = new ZipArchive( zipFile );

  // Wrap the operations that modify the zip archive in a batch update
  using( AutoBatchUpdate batch = new AutoBatchUpdate( zip ) )
  {
    // Select a source folder
    AbstractFile sourceFile = new DiskFile( @"D:\Data\File1.dat" );

    // Zip the files in the source folder into the zip archive
    sourceFile.CopyTo( zip, true );
  }

  /* To access the zipped data in memory, the application will open the
     zipped item for reading. Reading unzips data.
     Data will be read using a Stream object. Streaming allows data to be
     processed little by little, without having to create a large array to
     contain the entire data. */

  // Get the zipped item from the archive
  AbstractFile zippedFileInMemory = zip.GetFile( "File1.dat" );

  // Open the zipped item for reading
  using( Stream stream = zippedFileInMemory.OpenRead() )
  {
    // Create a read buffer
    int bufferLength = 8 * 1024;
    byte[] buffer = new byte[ bufferLength ];
    int read;

    // Attempt to unzip data
    read = stream.Read( buffer, 0, bufferLength );

    // While there is data to read
    while( read > 0 )
    {
      // TODO: Perform desired operation with the data read from the stream

      // Attempt to unzip more data
      read = stream.Read( buffer, 0, bufferLength );
    }
  }
}

Things you should consider

The main questions you should ask yourself when copying items 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.