5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellでワンクリック!ZIP一括展開&平坦化まとめ手順

Posted at

ChatGPT Image 2025年6月8日 00_39_58.png

ZIPファイルの中にZIPファイル、ひとつひとつクリックしてコピペしてまとめる
イライラする!!!
そんなイライラを解決する
簡単にぜーんぶ一つのフォルダに展開して欲しい のやり方っす
生成AIに聞けば、こんな事も自動化できるんですね(笑)

前提条件

  • Windows 7以降(PowerShell v2以上)が利用可能なPC
  • デスクトップに以下のフォルダをあらかじめ作成
    • ZIP (展開前のファイルを置く)
    • open(展開後をまとめる)

スクリプトの準備

  1. テキストエディタで新規ファイルを開く
  2. 以下をコピペして
    $sourceFolder = 'C:\Users<ユーザー名>\Desktop\ZIP'
    $destinationFolder = 'C:\Users<ユーザー名>\Desktop\open'
    のファイルパスを実際の"ZIP""oprn"フォルダのパスに変更
  3. UnpackAll.ps1 として保存(ファイル名を間違えないよう注意)

フォルダパスの調べ方(Windows)

  • エクスプローラーで対象のフォルダを開く

  • アドレスバーをクリックするとパスがテキスト表示に切り替わる

  • Ctrl + C でコピーし、UnpackAll.ps1 の該当箇所に貼り付け

  • 例: C:\Users\saway\Desktop\ZIP のように、\ をエスケープせずそのまま貼り付けてOKです。

# Filename: UnpackAll.ps1
# ------------ 設定 ------------
$sourceFolder      = 'C:\Users\<ユーザー名>\Desktop\ZIP'
$destinationFolder = 'C:\Users\<ユーザー名>\Desktop\open'

# ------------ 準備 ------------
Add-Type -AssemblyName System.IO.Compression.FileSystem
$shiftJIS = [System.Text.Encoding]::GetEncoding(932)

if (-not (Test-Path $destinationFolder)) {
    New-Item -ItemType Directory -Path $destinationFolder | Out-Null
}

# ------------ 平坦展開用関数 ------------
function Expand-ZipFlatten {
    param($zipPath, $outDir)

    Write-Host "展開 (平坦/SJIS):" $zipPath

    # 一時フォルダを作成
    $temp = Join-Path $env:TEMP ([GUID]::NewGuid().ToString())
    New-Item -ItemType Directory -Path $temp | Out-Null

    try {
        # SJIS 指定で全展開
        [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $temp, $shiftJIS)

        # 一時フォルダ内のすべてのファイルを平坦化コピー
        Get-ChildItem -Path $temp -Recurse -File | ForEach-Object {
            $target = Join-Path $outDir $_.Name
            Copy-Item -LiteralPath $_.FullName -Destination $target -Force -ErrorAction SilentlyContinue
        }
    }
    finally {
        # 一時フォルダを丸ごと削除
        Remove-Item -LiteralPath $temp -Recurse -Force
    }
}

# ------------ メイン処理 ------------

# 1) 元フォルダ内の ZIP を平坦展開+削除
Get-ChildItem -Path $sourceFolder -Recurse -Filter '*.zip' | ForEach-Object {
    Expand-ZipFlatten $_.FullName $destinationFolder
    Remove-Item -LiteralPath $_.FullName -Force
}

# 2) 元フォルダ内の非ZIPをまとめコピー
Get-ChildItem -Path $sourceFolder -Recurse -File |
    Where-Object { $_.Extension -ne '.zip' } |
    Copy-Item -Destination $destinationFolder -Force

# 3) ネストZIPにも同じ処理をループ
do {
    $nested = Get-ChildItem -Path $destinationFolder -Filter '*.zip'
    if ($nested.Count -gt 0) {
        $nested | ForEach-Object {
            Expand-ZipFlatten $_.FullName $destinationFolder
            Remove-Item -LiteralPath $_.FullName -Force
        }
    }
} while ($nested.Count -gt 0)

Write-Host "▶ すべてのファイルを '$destinationFolder' に平坦にまとめました!"

実行方法

PowerShell を起動して初回のみ実行

Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

スクリプト配置先に移動


cd C:\Users\<ユーザー名>\Desktop

スクリプト実行


.\UnpackAll.ps1

または一時的にバイパス

powershell -ExecutionPolicy Bypass -File .\UnpackAll.ps1

実行例

展開 (平坦/SJIS): C:\Users\saway\Desktop\ZIP\xxx.zip
▶ すべてのファイルを 'C:\Users\saway\Desktop\open' に平坦にまとめました!

FAQ

  • 管理者権限が必要?
    → CurrentUser スコープの実行ポリシーなら不要です。
  • 文字化けする?
    → ZIP が Shift-JIS 以外で圧縮されている場合は、
    [System.Text.Encoding]::GetEncoding(932)932 を適切なコードページに変更してください。
  • PowerShell が使えない場合
    → 7-Zip CLI (7z.exe) を使うバッチファイル版を検討してください。
5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?