2017-01-24 3 views
0

私は、ディレクトリとそのサブディレクトリのアーカイブを自動化するためにpowershellスクリプトを使用しています。私はほとんどの部分については、非常に単純なPowershellのAniooly with io.compression.zipfile

Add-Type -AssemblyName "system.io.compression.filesystem" 
    [io.compression.zipfile]::CreateFromDirectory($d, $destinationFilename) 

を使用しています、これは私はそれが何をする必要があるか正確にやっています。最近では、空のサブディレクトリに問題があります。これらの空のサブディレクトリは、何らかの形でディレクトリではなくファイルとして圧縮されています。

  1. まず、他に誰かがこれに遭遇しましたか?
  2. この.Net APIに関連する問題ですか、または再生時に環境問題がありますか?
  3. :: CreateFromDirectory関数の別のバージョンを使用する必要がありますか?

ありがとうございました!

+0

とみなされます。https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.archive/compress-archive? – jessehouwing

+0

私はこれを試してみることです。ありがとう! .NETフレームワークライブラリ以外のものを使用していますか? – halciber

+0

これが発生するサンプルのディレクトリ構造を記述できますか?複製できないようです –

答えて

2

私はBryanによってこのスクリプトを使用していますが、それはあなたの要求を満たす必要があります。

あなたが圧縮タイプ、タイムスタンプのように利用できる多くのパラメータがありますが、

# Purpose: Creates a .zip file of a file or folder. 
# 
# Sample: zipstuff.ps1 -target "C:\Projects\wsubi" -zip_to "C:\Users\Bryan\Desktop\wsubi" [-compression fast] [-timestamp] [-confirm] 
# 
# Params: 
# -target: The file or folder you would like to zip. 
# 
# -zip_to: The location where the zip file will be created. If an old version 
# exists, it will be deleted. 
# 
# -compression (optional): Sets the compression level for your zip file. Options: 
# a. fast - Higher process speed, larger file size (default option). 
# b. small - Slower process speed, smaller file size. 
# c. none - Fastest process speed, largest file size. 
# 
# -add_timestamp (optional): Applies a timestamp to the .zip file name. 
# By default, no timestamp is used. 
# 
# -confirm (optional): When provided, indicates that you would like to be 
# prompted when the zip process is finished. 
# 
# |Info| 

[CmdletBinding()] 
Param (
    [Parameter(Mandatory=$true,Position=0)] 
    [string]$target, 

    [Parameter(Mandatory=$true,Position=1)] 
    [string]$zip_to, 

    [Parameter(Mandatory=$false,Position=2)] 
    [ValidateSet("fast","small","none")] 
    [string]$compression, 

    [Parameter(Mandatory=$false,Position=3)] 
    [bool]$timestamp, 

    [Parameter(Mandatory=$false,Position=4)] 
    [bool]$confirm 
) 

#-----------------------------------------------------------------------------# 
function DeleteFileOrFolder 
{ Param([string]$PathToItem) 

    if (Test-Path $PathToItem) 
    { 
    Remove-Item ($PathToItem) -Force -Recurse; 
    } 
} 

function DetermineCompressionLevel{ 
[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll') 
Add-Type -Assembly System.IO.Compression.FileSystem 
    $CompressionToUse = $null; 

    switch($compression) 
    { 
    "fast" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest} 
    "small" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal} 
    "none" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::NoCompression} 
    default {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest} 
    } 

    return $CompressionToUse; 
} 

#-----------------------------------------------------------------------------# 
Write-Output "Starting zip process..."; 

if ((Get-Item $target).PSIsContainer) 
{ 
    $zip_to = ($zip_to + "\" + (Split-Path $target -Leaf) + ".zip"); 
} 
else{ 

    #So, the CreateFromDirectory function below will only operate on a $target 
    #that's a Folder, which means some additional steps are needed to create a 
    #new folder and move the target file into it before attempting the zip process. 
    $FileName = [System.IO.Path]::GetFileNameWithoutExtension($target); 
    $NewFolderName = ($zip_to + "\" + $FileName) 

    DeleteFileOrFolder($NewFolderName); 

    md -Path $NewFolderName; 
    Copy-Item ($target) $NewFolderName; 

    $target = $NewFolderName; 
    $zip_to = $NewFolderName + ".zip"; 
} 

DeleteFileOrFolder($zip_to); 

if ($timestamp) 
{ 
    $TimeInfo = New-Object System.Globalization.DateTimeFormatInfo; 
    $CurrentTimestamp = Get-Date -Format $TimeInfo.SortableDateTimePattern; 
    $CurrentTimestamp = $CurrentTimestamp.Replace(":", "-"); 
    $zip_to = $zip_to.Replace(".zip", ("-" + $CurrentTimestamp + ".zip")); 
} 

$Compression_Level = (DetermineCompressionLevel); 
$IncludeBaseFolder = $false; 

[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem"); 
[System.IO.Compression.ZipFile]::CreateFromDirectory($target, $zip_to, $Compression_Level, $IncludeBaseFolder); 

Write-Output "Zip process complete."; 

if ($confirm) 
{ 
    write-Output "Press any key to quit ..."; 
    $quit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown"); 
} 

使用方法を確認してください。

zipstuff.ps1 -target "C:\Projects\wsubi" -zip_to "C:\Users\Bryan\Desktop\wsubi" [-compression fast] [-timestamp] [-confirm] 

はそれがお役に立てば幸いです。

+1

うわー! Ranadipありがとう、私はこの例を本当に感謝します。私は今これを試してみるつもりです。あなたは私が遭遇した問題を経験しましたか?もしそうなら、このスクリプトは問題を解決しましたか? – halciber

+0

@halciber:なぜ私が 'zipfldr.dll'を使い始めたのか、ドットネットの限界を知っています。それがない場合は、外部からダウンロードして、 'C:\ WINDOWS \ System32 \ zipfldr.dll'スクリプトで与えたパスを与えることができます:) –