I problemi dell'oggetto GZipStream

Il .NET Framework, a partire dalla versione 2.0, fornisce il namespace System.IO.Compression, al cui interno troviamo, tra le altre, la classe GZipStream, con cui è possibile comprimere e decomprimere file in formato ZIP. Facendo qualche prova, tuttavia, ho notato che essa non garantisce un elevato livello di compressione, per usare un eufemismo... Il dubbio mi è venuto quando ho compresso un file PDF di 4,48 MB, ottenendo uno ZIP di 4,44 MB; mi sembrava un po' strano, quindi ho provato ad utilizzare WinRAR, che ha prodotto un archivio di appena 1,46 MB. Allora ho scaricato SharpZipLib: comprimendo lo stesso file con questa libreria, ho in effetti ottenuto un file di 1,46 MB. Per avere un riferimento, queste sono le due routine che ho utilizzato:

private static void CompressWithNET(string sourceFileName, string destinationFileName) { using (FileStream fs = new FileStream(destinationFileName, FileMode.Create)) { FileStream source = File.OpenRead(sourceFileName); byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); source.Close(); //Crea lo stream compresso. GZipStream zip = new GZipStream(fs, CompressionMode.Compress, false); //Comprime i dati. zip.Write(buffer, 0, buffer.Length); //Chiude lo stream. zip.Close(); } } public static void CompressWithSharpZipLib(string sourceFileName, string compressedFileName) { //Comprime il file specificato. using (ZipOutputStream zipStream = new ZipOutputStream( File.Create(compressedFileName))) { FileStream fs = File.OpenRead(sourceFileName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); //Crea una nuova entry nel file ZIP. ZipEntry entry = new ZipEntry(Path.GetFileName(sourceFileName)); entry.DateTime = File.GetLastWriteTime(sourceFileName); entry.Size = fs.Length; fs.Close(); Crc32 Crc = new Crc32(); Crc.Update(buffer); entry.Crc = Crc.Value; zipStream.PutNextEntry(entry); zipStream.Write(buffer, 0, buffer.Length); //La compressione è terminata. zipStream.Finish(); } }

Da una rapida ricerca su Internet, sembra che il problema dello GZipStream abbia a che fare con la gestione interna del buffer utilizzato per mantenere i dati della compressione. Indagherò, ma nel frattempo continuerò ad utilizzare SharpZipLib.

Technorati Tags: , ,

posted @ domenica 23 novembre 2008 21.55

Print

Comments on this entry:

# re: I problemi dell'oggetto GZipStream

Left by Matteo Baglini at 24/11/2008 10.11
Gravatar
se puoi usa la libreria DotNetZip

# re: I problemi dell'oggetto GZipStream

Left by Marco Minerva at 24/11/2008 10.15
Gravatar
Perché? Dici che è più efficiente? L'hai provata?

# SharpZipLib 1, DotNetZip 0

Left by Around and About .NET World at 26/11/2008 0.37
Gravatar
Dopo aver letto il mio post sui problemi con l'oggetto GZipStream , l'amico Matteo Baglini mi

# re: I problemi dell'oggetto GZipStream

Left by Cheeso at 13/02/2009 18.27
Gravatar
>Perche?
You would use DotNetZip to avoid all the complexity of the SharpZipLib interface.

public static void CompressWithDotNetZip(string sourceFileName, string compressedFileName)
{
using (var zip = new ZipFile())
{
zip.AddFile(sourceFileName);
zip.Save(compressedFileName);
}
// done!
}

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 3 and type the answer here:
 

Live Comment Preview:

 
«marzo»
domlunmarmergiovensab
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910