tech.guitarrapc.cóm

Technical updates

PowerShell v5 の新機能紹介 - Zipファイルの操作が可能に

今回も前回に続きPowerShell v5をコアとするWindows Management Framework 5.0 Preview September 2014 (v5.0) の機能詳細を触りつつ、何が変わるのかメモです。

  • Manage .ZIP files with new cmdlets

つまり、 Zipファイルが標準で扱えるようになります。Windowsようやくここまで来たか感ですね。

  • 過去の記事はこちら

https://tech.guitarrapc.com/entry/2014/09/05/065912

https://tech.guitarrapc.com/entry/2014/09/08/042747

https://tech.guitarrapc.com/entry/2014/09/08/050311

Cmdlet

対象のCmdletは2つです。

CommandType Name             Version Source
----------- ----             ------- ------
Function    Compress-Archive 1.0.0.0 Microsoft.PowerShell.Archive
Function    Expand-Archive   1.0.0.0 Microsoft.PowerShell.Archive

Compress-Archive

圧縮します。

NAME
    Compress-Archive

SYNOPSIS
    The Compress-Archive cmdlet can be used to zip/compress one or more files/directories.

SYNTAX
    Compress-Archive [-Path] <String[]> [-DestinationPath] <String> [-CompressionLevel <String>] [-Update] [-WhatIf] [-
    Confirm] [<CommonParameters>]

    Compress-Archive -LiteralPath <String[]> [-DestinationPath] <String> [-CompressionLevel <String>] [-Update] [-WhatI
    f] [-Confirm] [<CommonParameters>]


DESCRIPTION


PARAMETERS
    -Path <String[]>

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false

    -LiteralPath <String[]>

        Required?                    true
        Position?                    named
        Default value
        Accept pipeline input?       true (ByPropertyName)
        Accept wildcard characters?  false

    -DestinationPath <String>

        Required?                    true
        Position?                    2
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -CompressionLevel <String>

        Required?                    false
        Position?                    named
        Default value                Optimal
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Update [<SwitchParameter>]

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -WhatIf [<SwitchParameter>]

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Confirm [<SwitchParameter>]

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS

OUTPUTS


RELATED LINKS

ファイル圧縮

例えば、 D:\hoge.logをd:\hoge.zipに圧縮したいなら?

Compress-Archive -Path D:\hoge.log -DestinationPath d:\hoge.zip

生成されました。

    Directory: D:\


Mode          LastWriteTime Length Name
----          ------------- ------ ----
-a---- 2014/09/08      5:10 213260 hoge.zip

中身を見てみましょう。

image

フォルダ圧縮

フォルダの圧縮します。例えば、D:\GitHub\valentia\Valentaiをd:\valentia.zipに圧縮してみましょう。 こんな構造です。

image

実行してみましょう。ファイルと変わりません。判定は自動的に行ってくれます。

Compress-Archive -Path D:\GitHub\valentia\valentia -DestinationPath D:\valentia.zip

サイズがそれなりだったので、実行するとプログレスバーが出ました。

image

生成されたzipを見てみると、指定したパスの構造そのままにできている?

image

と思いきや、Lhazなど3rdパーティソフトで見ると、フォルダがファイルになっていたりして構造が壊れています。

image

一方で、Windows標準のExplorerなら平気です。

image

んー、微妙なのでConnectにはあげませんでしたが、どうしたものか。

条件は容易で、「ファイルを含まないフォルダが存在していると、ルートのフォルダがファイルに変わります」

再現コードです。

# Reproduce
mkdir d:\hoge\hoge\1\2\3\4
mkdir d:\hoge\hoge\1\2\3\5
New-Item d:\hoge\hoge\log.log
New-Item d:\hoge\hoge\1\2\3\3.log
New-Item d:\hoge\hoge\1\2\3\4\4.log
Compress-Archive d:\hoge\hoge -DestinationPath d:\hoge.zip

基本的に、 Windows標準のExplorerは逆に他で利用できないZipを生成するので、 LhazやWinRARなどはその辺安定しているので微妙ですね。

解凍

解凍も簡単です。Expand-Archive使います。

Compress-Archiveに対して、Expland-Archiveはうまく解凍してくれるのですばらです。

例えば、c:\に解凍するならこうです。

Expand-Archive -Path d:\valentia.zip -DestinationPath c:\

image

Zipへのアイテム追加

Compress-Archiveには、 Updateスイッチがあります。

これを使うことで、既存のzipにアイテムを追加可能です。

例えば、 d:\valentia.zipに、 d:\GitHub\DayBreakJPフォルダを追加するならこうです。

Compress-Archive -Path D:\GitHub\DayBreakJP -DestinationPath D:\valentia.zip -Update

うまく追加されていますね!

image

まとめ

Zipの処理には、 DSCのArchiveリソースという手段もありましたが、Cmdletでできるのはうれしいですね!