by Andrea
04 July 2007 21:07
In un progettino che stò sviluppando avevo la necessità di fare l'upload e scompattare un archivio zip. Per fare ciò mi sono appoggiato alla liberia #ziplib che si è dimostrata di una facilità estrema, infatti con poche righe di codice sono riuscito nel mio intento:
/// <summary>
/// Unzip the zip file.
/// </summary>
/// <param name="fileZipPath">The path of file zip.</param>
private void UnZipFile(string fileZipPath)
{ using (ZipInputStream s = new ZipInputStream(File.OpenRead(fileZipPath)))
{ ZipEntry entry;
while ((entry = s.GetNextEntry()) != null)
{ // Prelevo solo il nome del file all'interno dello zip
string fileName = Path.GetFileName(entry.Name);
// Combino il path su disco con quello che il file ha all'interno del file zip
string filePath = Path.Combine(Path.GetDirectoryName(fileZipPath), entry.Name);
// Se la directory non esiste la creo
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
Directory.CreateDirectory(filePath);
if (fileName != String.Empty)
using (FileStream streamWriter = File.Create(filePath))
{ int size = 2048;
byte[] data = new byte[2048];
while (true)
{ size = s.Read(data, 0, data.Length);
if (size > 0)
{ streamWriter.Write(data, 0, size);
}
else
{ break;
}
}
}
}
}
}
Credo (appena avrò tempo) di approfondire un pò meglio questa libreria perchè mi sembra proprio fatta bene.
Ecco il link alla pagina di downlad.
Technorati tags:
#ziplib,
Zip,
UnZip
037b17a4-c041-45ee-b2b5-0575363f1f30|0|.0
Tags:
.NET