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:
-
Specify the zip file to create or add files to. To do this, set the ZipFilename property.
-
If you also have real files to zip up as well as blocks of memory, you must specify these real files. To do this, set the FilesToProcess property.
-
Provide the filename(s) and file information for the block(s) of memory to compress into the zip file. To do this, write a handler for the QueryMemoryFile event.
-
Tell Xceed Zip to start zipping. To do this, call the Zip method.
-
Provide the data for the filename(s) you specified to compress from memory. To do this, write a handler for the ZippingMemoryFile event.
-
Make sure it worked successfully. To do this, check the Zip method's return value.
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..."; } |