LoginSignup
4
7

More than 5 years have passed since last update.

PowerShell 5.0で2GB以上のファイルをzip圧縮する

Last updated at Posted at 2016-07-27

 PowerShell 5.0からCompress-Archiveが追加されて、cmdletから簡単にzipファイルが作成できるようになった。

Compress-Archive src\dir dest.zip

しかし、まだ制限事項があり、Technetによると、

TN Compress-Archive

Because Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitation of the underlying API.

とあり、現状、2GBのファイルまでしか圧縮できないようだ。
 もっとも、別に.NET Framework 4.5にそのような制限があるわけではないので、2GB以上のファイルを圧縮したいのであれば、直接フレームワークの機能を使ってしまえばよい。

Add-Type -AssemblyName System.IO.Compression.FileSystem

[IO.Compression.ZipFile]::CreateFromDirectory(src\dir, dest.zip)

さらに細かく書くなら、

Add-Type -AssemblyName System.IO.Compression.FileSystem

[IO.Compression.ZipFile]::CreateFromDirectory(src\dir, dest.zip, [IO.Compression.CompressionLevel]::Optimal, $true, [Text.Encoding]::Default)

としてエンコードをShift-JISにすれば、現状のCompress-Archiveでは化けてしまう、日本語のファイル名やディレクトリ名も保存できる。

日本語版が出る頃には、この辺りも改善されてるといいな。

4
7
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
4
7