Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Zip to Memory, Unzip from Memory
This C# sample program demonstrates zipping and unzipping from memory. public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
private void button1_Click(object sender, System.EventArgs e)
{
Chilkat.Zip zip = new Chilkat.Zip();
zip.UnlockComponent("30-day trial");
// Initialize the Zip object. This does not create a file on disk.
zip.NewZip("test.zip");
// Generate some data.
String fileContents = "Test\n";
int i;
for (i=0; i<100; i++)
{
fileContents = fileContents + "This is a test 1234\n";
}
byte [] byteData = StrToByteArray(fileContents);
textBox1.Text = "uncompressed data size = " + byteData.Length + "\r\n";
// Append this data to the Zip object.
zip.AppendData("test.txt",byteData);
// Get the compressed data.
Chilkat.ZipEntry entry;
// The first entry is what we just appended.
entry = zip.FirstEntry();
// Copy the compressed data without decompressing it.
byte [] compressedData = entry.Copy();
textBox1.Text = textBox1.Text + "compressed data size = " + compressedData.Length + "\r\n";
// ...
// Send the compressed data over a network connection, for example...
// ...
// This could be the code in another program that receives
// the compressed data.
Chilkat.Zip zip2 = new Chilkat.Zip();
// Initialize the Zip object. This does not create a file on disk.
zip2.NewZip("test.zip");
// Append the already-compressed data.
zip2.AppendCompressed("test.txt",compressedData);
// Extract it to a file named "test.txt" in the current directory.
bool success;
success = zip2.Extract(".");
if (!success)
{
MessageBox.Show(zip2.LastErrorText);
}
else
{
MessageBox.Show("Extracted to test.txt");
}
}
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.