Login  
Search All Forums
Dart Home | PowerTCP Zip Compression for .NET | Custom Development Reply | PowerTCP Zip Compression for .NET Topics | Forums   
AuthorForum: PowerTCP Zip Compression for .NET
Topic: Zip to file produces archives containing void files
idngarda

From: Brescia, Italy
Posts: 2
Member Since: 08/08/08
posted August 8, 2008 11:48 AM

I'm trying to generate "on-demand" zip files using the Zip(string destination) method. Here is my sample code:

static void Main(string[] args)
{
//creates a new archive
Archive a = new Archive();

//creates a new memory stream and stream writer
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);

//adds something to the memory stream
sw.Write("Hallo World!");
sw.Flush();

//creates a new archive item and adds it to the archive
ArchiveItem item1 = new ArchiveItem(ms);
item1.Name = "SampleFile.txt";
a.Add(item1);

//zips the archive to a new memory stream
MemoryStream streamArchive = new MemoryStream();
a.Zip("c:\\sample.zip");
}

After the "sw.Write("Hallo World!")" command, ms.Length equals 12.
After the "ArchiveItem item1 = new ArchiveItem(ms)" command, item1.Size equals 12, and item1.CompressedSize equals 0.
After the "a.Add(item1)" command, a.Count equals 1.
After the "a.Zip("c:\\sample.zip")" command, item1.Size equals 12, and item1.CompressedSize equals 2. The file size of sample.zip is 128 byte.
If I open sample.zip with my WinZip, il will find SampleZip.txt inside. The file size of sample.zip is 12 byte, but its content is totally void (I cannot find my "Hallo World"!).

Where am I wrong? The version I am using is 2.0.0.6

Many thanks!
Jason Farrar (Admin)

From: USA
Posts: 223
Member Since: 07/24/07

Extra Support Options
Custom Application Development

posted August 12, 2008 11:35 AM

after sw.flush() you need to add:
ms.Position = 0;

The position of the memory stream is at the end of the stream because that is where it is after the stream is written to. We don't reset it automatically because this provides the ability to write only a segment of the stream to the zip by setting the memory stream position before adding it to the archive.

------
-Non-current subscribers must contact sales@dart.com to update subscription and receive continued support as needed.
------

idngarda

From: Brescia, Italy
Posts: 2
Member Since: 08/08/08
posted August 12, 2008 11:42 AM

Many thanks! Now it works correctly!
An advice: add this tip in the help file!
Reply | PowerTCP Zip Compression for .NET Topics | Forums   
This site is powered by PowerTCP WebServer Tool PowerTCP WebServer for ActiveX