LoginSignup
2
3

More than 5 years have passed since last update.

PowerShell による CAB 解凍&圧縮

Last updated at Posted at 2016-03-30

InfoPathフォーム(*.xsn)の中身を書き換えるのに使いました。

解凍

# 解凍したいファイルを指定
$FilePath = "(ファイルパス)"

# 解凍先フォルダを指定
$extractedFolderPath = "(フォルダパス)"


# 使いやすいようにファイル情報クラスを作成
$fi = New-Object IO.FileInfo($FilePath);

# 解凍先フォルダ作成
$tempDir = New-Item ($extractedFolderPath) -ItemType Directory -Force

# CABファイルを解凍
expand ("""" + $fi.FullName+ """") -F:* $tempDir

圧縮

# 圧縮後のファイルを指定
$FilePath = "(ファイルパス)"

# 圧縮するフォルダを指定
$archivingFolderPath = "(フォルダパス)"


# 使いやすいようにファイル情報クラスを作成
$fi = New-Object IO.FileInfo($FilePath);

# FSOを作成(短い形式のフォルダを作るため)
$fso = New-Object -ComObject Scripting.FileSystemObject

# テンポラリフォルダに ddf ファイルのパスを生成
$ddfFile = [System.IO.Path]::GetTempFileName()

# ddfファイルの内容を作成
$ddfHeader =@"
;*** MakeCAB Directive file
;
.OPTION EXPLICIT 
.Set CabinetNameTemplate=temp.cab
.Set DiskDirectory1={0}
.Set MaxDiskSize=CDROM
.Set Cabinet=on
.Set Compress=on
"@ -F $fso.GetFolder($fi.Directory.FullName).ShortPath

# ddf ファイルを生成
$ddfHeader | Out-File -filepath $ddfFile -force -encoding oem

# 圧縮するフォルダの内容を ddf ファイルに書き出す
$tempDir.GetFiles() | %{ 
    '"' + $_.Fullname + '"' | 
    Out-File -FilePath $ddfFile -Encoding ASCII -Append
}

# ddf ファイルを指定して圧縮する
makecab /f $ddfFile
# ddf ファイルを削除
del $ddfFile

# ファイル名を整える(ddf ファイルにマルチバイトのファイル名が指定できないので、一時的に temp.cab で作成した後に直す)
$fi.Directory.GetFiles("temp.cab")[0].MoveTo($fi.Name)

# ごみが出るので削除
$fi.Directory.GetFiles("setup.*") | Remove-Item
2
3
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
2
3