C# Language                
            .zipファイルの読み書き
        
        
            
    サーチ…
構文
- public static ZipArchive OpenRead(ストリングarchiveFileName)
 
パラメーター
| パラメータ | 詳細 | 
|---|---|
| archiveFileName | オープンするアーカイブへのパス。相対パスまたは絶対パスで指定します。相対パスは現在の作業ディレクトリからの相対パスとして解釈されます。 | 
zipファイルへの書き込み
新しい.zipファイルを書き込むには:
System.IO.Compression
System.IO.Compression.FileSystem
using (FileStream zipToOpen = new FileStream(@"C:\temp", FileMode.Open)) 
{
    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) 
    {
        ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
        using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) 
        {
            writer.WriteLine("Information about this package.");
            writer.WriteLine("========================");
        }
    }
}
        Zipファイルをメモリに書き込む
次の例では、ファイルシステムにアクセスすることなく、提供されたファイルを含むzipファイルのbyte[]データを返します。 
public static byte[] ZipFiles(Dictionary<string, byte[]> files)
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Update))
        {
            foreach (var file in files)
            {
                ZipArchiveEntry orderEntry = archive.CreateEntry(file.Key); //create a file with this name
                using (BinaryWriter writer = new BinaryWriter(orderEntry.Open()))
                {
                    writer.Write(file.Value); //write the binary data
                }
            }
        }
        //ZipArchive must be disposed before the MemoryStream has data
        return ms.ToArray();
    }
}
        Zipファイルからファイルを取得する
この例では、提供されたzipアーカイブバイナリデータからファイルのリストを取得します。
public static Dictionary<string, byte[]> GetFiles(byte[] zippedFile) 
{
    using (MemoryStream ms = new MemoryStream(zippedFile))
    using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read)) 
    {
        return archive.Entries.ToDictionary(x => x.FullName, x => ReadStream(x.Open()));
    }
}
private static byte[] ReadStream(Stream stream) 
{
    using (var ms = new MemoryStream()) 
    {
        stream.CopyTo(ms);
        return ms.ToArray();
    }
}
        次の例は、zipアーカイブを開いて、すべての.txtファイルをフォルダに展開する方法を示しています
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\example\start.zip";
            string extractPath = @"c:\example\extract";
            using (ZipArchive archive = ZipFile.OpenRead(zipPath))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                    {
                        entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
                    }
                }
            } 
        }
    }
}
    
    
    
    
    Modified text is an extract of the original Stack Overflow Documentation
        ライセンスを受けた CC BY-SA 3.0
        所属していない Stack Overflow