LoginSignup
1
0

More than 3 years have passed since last update.

ZIPファイルの中身を解凍する

Last updated at Posted at 2021-04-17

先日、windowsのCドライブのディスクが枯渇する問題がありました。
原因は凡ミスでTEMPファイルを大量生成していたのですが。。。
その関連でCドライブの%TEMP%にファイルを作成せずにZIPファイルを解凍する方法があったので書き込みます。

まず、ZipArchiveを使えるようにするため、以下を参照します。

  • System.IO.Compression
  • System.IO.Compression.FileSystem

1.png

そして、以下のコードを書きます。

    var file = @"comp.zip";
    using (ZipArchive archive = ZipFile.OpenRead(file))
    {
        var f = archive.GetEntry("comp.txt");
        var ms = f.Open();
        using (var fileStream = new FileStream("output.txt",
            FileMode.CreateNew, FileAccess.ReadWrite))
        {
            ms.CopyTo(fileStream);
        }
    }

これで、comp.zipにあるcomp.txtをoutput.txtに書き出すことができます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0