Xceed Zip Compression Library Documentation
Zipping files from memory directly to a zip file
Getting started quickly > Zipping files from memory directly to a zip file

Compression from memory to a zip file

Xceed Zip allows you to compress blocks of memory into files in a zip file. To accomplish this, perform the following 6 steps:

Below you will find a VB, Delphi and C# examples.

Here is sample code for these 6 steps (excluding the code for the QueryMemoryFile and ZippingMemoryFile events):

Visual Basic Copy Code

' Don't forget to put an Xceed Zip control on a form
Dim ResultCode As xcdError 

Call XceedZip1.License( "your license key" )

XceedZip1.ZipFilename = "c:\outgoing\pictures.zip"
XceedZip1.FilesToProcess = "c:\graphics\cars\*.bmp" 

ResultCode = XceedZip1.Zip 

If ResultCode = xerSuccess Then
   MsgBox "Files zipped successfully." 
Else
   MsgBox XceedZip1.GetErrorDescription(xvtError, ResultCode) 
EndIf

Delphi Copy Code

var xErr : xcdError; 
begin

   XceedZip1.License( 'your license key' );
   XceedZip1.ZipFilename := 'c:\outgoing\pictures.zip'; 
   XceedZip1.FilesToProcess := 'c:\graphics\cars\*.bmp';  

   xErr := XceedZip1.Zip; 
   ShowMessage( XceedZip1.GetErrorDescription( xvtError, xErr ) ); 
end;

C# Copy Code

//Subscribe to the ZippingMemoryFile and the QueryMemoryFile events

this.zip.License( @"your license key" );

this.zip.ZippingMemoryFile += new AxXceedZipLib._IXceedZipEvents_ZippingMemoryFileEventHandler(this.zip_ZippingMemoryFile);

this.zip.QueryMemoryFile += new AxXceedZipLib._IXceedZipEvents_QueryMemoryFileEventHandler(this.zip_QueryMemoryFile); 

zip.ZipFilename = @"c:\pictures.zip";
zip.FilesToProcess = @"c:\graphics\cars\*.bmp"; 

xcdError ResultCode = zip.Zip();

if(  ResultCode == xcdError.xerSuccess  )
{
   MessageBox.Show("Files zipped successfully." );
}
else
{
   MessageBox.Show( zip.GetErrorDescription( xcdValueType.xvtError, ( int )ResultCode ) );
}

Here is sample code to put into the QueryMemoryFile event handler. This sample assumes you only have a single file to zip up from memory:

Visual Basic Copy Code

sFilename = "Readme.txt" ' necessary 

If lUserTag = 0 Then
   bFileProvided = True
Else
   bFileProvided = False
End If

Delphi Copy Code

if ( lUserTag = 0 ) then

begin
   sFilename := 'readme.txt'; 
   bFileProvided := true; 
end
else
   bFileProvided := false;

C# Copy Code
private void zip_QueryMemoryFile(object sender, AxXceedZipLib._IXceedZipEvents_QueryMemoryFileEvent e)

   e.sFilename = "Readme.txt"; 

   if (e.lUserTag == 0)

      e.bFileProvided = true; 

   else

      e.bFileProvided = false;
}

Here is sample code to put into the ZippingMemoryFile event handler:

Visual Basic Copy Code

vaDataToCompress = "Welcome to my special collection of " +

 "car pictures. These .bmp files can be loaded in MS Paint..."

bEndOfData = True

Delphi Copy Code

vaDataToCompress := 'Welcome to my special collection of ' +

'car pictures. These .bmp files can be ' + 

'loaded in MS Paint...'; 

bEndOfData := True;

C# Copy Code
private void zip_ZippingMemoryFile(object sender,
                                   AxXceedZipLib._IXceedZipEvents_ZippingMemoryFileEvent e)
{

   e.vaDataToCompress = "Welcome to my special collection of " + 
                        "car pictures. These .bmp files can be loaded in MS Paint...";
}