簡體   English   中英

將空文件夾壓縮為文件夾,而不是文件

[英]Compress empty folders as folders, not files

我使用System.IO.Compression.FileSystem在Powershell腳本中壓縮帶有子文件夾的文件夾。 一些文件夾是空的,並且它創建空文件而不是空文件夾。 我以為這是一個錯誤,但是他們在文檔中指定了它:

文件系統中的目錄結構保留在歸檔中。 如果目錄為空,則創建一個空的存檔。 使用此方法重載可以指定壓縮級別以及是否在歸檔文件中包括基本目錄。

您知道如何避免這種情況嗎,我希望將空文件夾作為文件夾...這似乎很荒謬...

編輯:

$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($Source, $Target, $compressionLevel, $true)

對我來說,如果我添加對System.IO.Compression.FileSystem.dll的引用,它就可以正常工作:

Add-Type -Path 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a5c561934e089\System.IO.Compression.FileSystem.dll'

# compress using just two parameters. 
# This will omit the source folder and gives you no option to set the CompressionLevel
[System.IO.Compression.ZipFile]::CreateFromDirectory("D:\Testzip", "D:\result.zip")

如果還希望包含源文件夾,請使用帶有4個參數的函數:

[System.IO.Compression.ZipFile]::CreateFromDirectory("D:\Testzip", "D:\result.zip", "Fastest", $true)

請參見CompressionLevel枚舉

為了進行測試,我使用了僅子文件夾D:\\Testzip\\folder1\\folder1-1為空的文件夾結構:

 D:\\TESTZIP | the only file in the root directory.txt | \\---folder1 +---folder1-1 \\---folder1-2 just a single file here.txt 

之后, result.zip文件就包含了一個空文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM