A DotNet Raider

My adventures in the .NET world!
posts - 38, comments - 15, trackbacks - 0

My Links

News

Website View Martino Bordin's profile on LinkedIn XBOX 360 AVATAR - BORD1986
Locations of visitors to this page

Archives

Post Categories

Arch

BlogRoll

Codeplex

Google code

MSDN

Sites

Thursday, August 23, 2012

Manipolazione di file zip con il framework 4.5

Dalla versione 4.5 del .NET framework è finalmente supportata out-of-the-box la manipolazione di file zip senza l'utilizzo di librerie di terze parti.

Sono ora disponibili infatti 2 assembly System.IO.Compression e System.IO.Compression.FileSystem che permettono, ad esempio, di enumerare i file presenti in uno zip, estrarli, creare nuovi file zip, etc.

Ad esempio, ecco come si ottiene l'elenco dei file contenuti in uno zip:

  const string zipFilePath = @"C:\temp\files.zip";

    using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open)){
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read)) {

        foreach (var zipArchiveEntry in archive.Entries) {
            Console.WriteLine("File name: {0}", zipArchiveEntry.FullName);}
    }}

E come si estraggono in una directory:

const string zipFilePath = @"C:\temp\files.zip";
    const string dirToExtract = @"C:\temp\";

    using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open)) {
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update))
        archive.ExtractToDirectory(dirToExtract);}}

Vi lascio quindi i riferimenti di MSDN:

ZipArchive Class

System.IO.Compression

 

 

posted @ Thursday, August 23, 2012 10:38 AM | Feedback (0) | Filed Under [ VISUAL STUDIO MISC ]

Powered by:
Powered By Subtext Powered By ASP.NET