Zippare file usando il Framework 2.0

martedì 08 luglio 2008 - 17.13

GuardianOfTheFlame Profilo | Junior Member

Ciao a tutti, ho scoperto che è possibile zippare/unzippare file aggiungendo al mio progetto il riferimento a vjslib.dll e importando java.util.zip

Ho fatto delle semplici prove e sembra essere ok: mi chiedevo però se questo sistema sia ritenuto obsoleto; io uso ancora VS 2005, ma ho letto che da VS2008 non sarà più supportato J#.
Ho pensato fosse meglio usare le librerie del framework, però se J# diventa obsoleto significa che è meglio usare una libreria di terze parti? in VS2008 la libreria vsjlib.dll esiste?

La semplice classe che uso comunque funziona e richiede solo l'installazione di vjredist.exe (visual j# redistributable package).

Salo

The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us - Calvin (and Hobbes)

Enne Profilo | Junior Member

Ciao Guardian,
Come hai detto anche tu J# non è supportato più in VS 2008 quindi bisognerebbe installare il J# Redistributable.
Se vuoi un mio consiglio utilizza le SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib) che contengono un'implementazione .NET delle librerie Java.


Rocco Verrastro
.NET Programmer - Web Developer

kopiro91 Profilo | Newbie

se non sbagli con la funzione Gzip in Syste.IO.COmpressFIle

Enne Profilo | Junior Member

Kopiro forse un errore tuo di scrittura System.IO.Compression.
Qui un piccolo esempio in C# usando GZipStream.

using System.IO; using System.IO.Compression; using System.Text; private void TestCompress() { string srcFile = @"C:\file.txt"; string dstFile = @"C:\file.gzip"; FileStream fsIn = null; FileStream fsOut = null; GZipStream gzip = null; byte[] buffer; int count = 0; try { fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None); gzip = new GZipStream(fsOut, CompressionMode.Compress, true); fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read); buffer = new byte[fsIn.Length]; count = fsIn.Read(buffer, 0, buffer.Length); fsIn.Close(); fsIn = null; gzip.Write(buffer, 0, buffer.Length); } catch (Exception ex) { System.Diagnostics.Debug.Assert(false, ex.ToString()); } finally { if (gzip != null) { gzip.Close(); gzip = null; } if (fsOut != null) { fsOut.Close(); fsOut = null; } if (fsIn != null) { fsIn.Close(); fsIn = null; } } } private void TestDecompress() { string srcFile = "C:\\temp\\compressed-file.gzip"; string dstFile = "C:\\temp\\decompressed-file.txt"; FileStream fsIn = null; FileStream fsOut = null; GZipStream gzip = null; const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int count = 0; try { fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read); fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None); gzip = new GZipStream(fsIn, CompressionMode.Decompress, true); while (true) { count = gzip.Read(buffer, 0, bufferSize); if (count != 0) { fsOut.Write(buffer, 0, count); } if (count != bufferSize) { // have reached the end break; } } } catch (Exception ex) { System.Diagnostics.Debug.Assert(false, ex.ToString()); } finally { if (gzip != null) { gzip.Close(); gzip = null; } if (fsOut != null) { fsOut.Close(); fsOut = null; } if (fsIn != null) { fsIn.Close(); fsIn = null; } } }

Rocco Verrastro
.NET Programmer - Web Developer

GuardianOfTheFlame Profilo | Junior Member

Grazie ragazzi!


The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us - Calvin (and Hobbes)

fguida Profilo | Expert

Ciao...stavo cercando qualcosa sulla Decompressione, ed ho trovato questo post interessante...domanda:

- Come mai si deve impostare il "Buffersize = 4096" ?

E' un punto che mi sfugge.
Grazie mille

Francesco
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5