2

I've encrypted .zip archive with some files. Later archive contents must be checked by someone who doesn't know encryption password. Is there any way to do this in powershell?

Ubuntu has zip -sf myfile.zip command but I couldn't find any simular in powershell.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
malene.dev
  • 35
  • 1
  • 7

2 Answers2

2

If you're just looking to list the zip contents, then this function will do. As for extracting the Zip contents, ZipArchive does not support encrypted Zips as of today. There are third party PowerShell Modules as well as libraries that can do this though.

function Get-ZipContent {
    [CmdletBinding()]
    param(
        [Parameter(ParameterSetName = 'Path', Position = 0, Mandatory, ValueFromPipeline)]
        [string[]] $Path,

        [Parameter(ParameterSetName = 'LiteralPath', Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('PSPath')]
        [string[]] $LiteralPath,

        [Parameter()]
        [switch] $Force
    )

    begin {
        Add-Type -AssemblyName System.IO.Compression
    }
    process {
        try {
            $arguments = switch($PSCmdlet.ParameterSetName) {
                Path { $Path, $Force.IsPresent, $false }
                LiteralPath { $LiteralPath, $Force.IsPresent, $true }
            }

            foreach($item in $ExecutionContext.InvokeProvider.Item.Get.Invoke($arguments)) {
                try {
                    $fs  = $item.OpenRead()
                    $zip = [IO.Compression.ZipArchive]::new($fs, [IO.Compression.ZipArchiveMode]::Read)
                    foreach($entry in $zip.Entries) {
                        $entry.PSObject.Properties.Add([psnoteproperty]::new('Source', $item.FullName))
                        $entry
                    }
                }
                catch {
                    $PSCmdlet.WriteError($_)
                }
                finally {
                    $zip, $fs | ForEach-Object Dispose
                }
            }
        }
        catch {
            $PSCmdlet.WriteError($_)
        }
    }
}

Usage:

PS ..\pwsh> Get-ZipContent path\to\myfolder\*.zip
PS ..\pwsh> Get-ChildItem path\to\things -Recurse -Filter *.zip | Get-ZipContent 

To further expand the usage since it seems not quite clear:

# load the function in memory:
PS ..\pwsh> . ./theFunctionisHere.ps1

# call the function giving it a path to a zip:
PS ..\pwsh> Get-ZipContent ./thing.zip

Source             : path/to/pwsh/thing.zip
Archive            : System.IO.Compression.ZipArchive       
Crc32              : 0
IsEncrypted        : True
CompressedLength   : 165
ExternalAttributes : 32
Comment            : 
FullName           : other thing.txt
LastWriteTime      : 10/29/2022 10:31:30 AM -03:00
Length             : 446
Name               : other thing.txt

Source             : path/to/pwsh/thing.zip
Archive            : System.IO.Compression.ZipArchive
Crc32              : 0
IsEncrypted        : True
CompressedLength   : 165
ExternalAttributes : 32
Comment            : 
FullName           : thing.txt
LastWriteTime      : 10/29/2022 10:31:30 AM -03:00
Length             : 446
Name               : thing.txt
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Thank you for help! But than I run your command nothing is printed out on a screen. Script runs withoun any errors but doesn't show anything. – malene.dev Oct 29 '22 at 21:12
  • @postanote not sure if your comment is directed to OP or to me. If it's to me, not sure what you mean by your comment. Zip provides encryption for reading the ZipEntries, does not provide obfuscation for listing the file structure (ZipArchives) afaik. – Santiago Squarzon Oct 29 '22 at 21:29
  • @postanote In my case user should be able to check what archive contains but could not unzip it. Why do i want it is is my own buisness – malene.dev Oct 29 '22 at 21:38
  • @malene are you saying `Get-ZipContent path\to\myzip.zip` does not output anything? Function works perfectly fine otherwise I wouldn't have posted an answer – Santiago Squarzon Oct 29 '22 at 21:46
  • @SantiagoSquarzon Yes... I copy-paste your code into Get-ZipContent.ps1 file and run it in powershell with command `./Get-ZipContent.ps1 ./archive.zip`. Script running without any errors but there is no output in shell. I tried script with non-encrypted zip in case encryption is the problem but got nothing again – malene.dev Oct 29 '22 at 21:52
  • Well then your problem is that you're not following the usage I've given in my answer, the `./` before `Get-ZipContent` and it's extension `.ps1` has nothing to you there. Re-read the __Usage__ part of the answer. @malene // `. ./Get-ZipContent.ps1` (two dots with a space between them) to load the function in memory, then just call the function by it's name not by the __file name__ – Santiago Squarzon Oct 29 '22 at 21:55
  • @malene i've added an example to make it clearer – Santiago Squarzon Oct 29 '22 at 22:03
  • 1
    My bad, I'm a newbie with powershell... didn't figure out what *..\pwsh>* part is. For anyone wondering `. Get-ZipContent /path/to/myzip.zip` worked for me! @SantiagoSquarzon Thanks once again for help! Worked perfectly!It is strange that there is so little information about encrypted-archives and powershell out there... – malene.dev Oct 29 '22 at 22:06
1

For speed the Windows command to list zip folders and contents is now tar, but it does not currently support passwords on command line as option (see end comment). BUT will show contents

f is file/folder t is test v is verbose x is extract (there are others for writing a zip)

C:\Programming>tar ft "sample (2).zip"
sample1.pdf
sample2.pdf

C:\Apps\Programming>tar ftv "sample (2).zip"
-rw-rw-r--  0 0      0        1298 Feb 17 00:46 sample1.pdf
-rw-rw-r--  0 0      0        1298 Feb 17 01:12 sample2.pdf

C:\Programming>

One limitation is that if you have zip in a zip you need to extract the first level so as to test the inner zips

C:\Programming>tar ftv "test (2).zip"
-rw-rw-r--  0 0      0     1355480 Apr 18 14:29 test.zip

C:\Programming>tar -xf "test (2).zip"&tar ftv test.zip
-rw-rw-r--  0 0      0      154239 Apr 18 14:20 test150DPIx24bit.jpg
-rw-rw-r--  0 0      0      159143 Apr 18 14:13 test150DPIx4bit.png
-rw-rw-r--  0 0      0      403059 Apr 18 14:27 test96DPI.jpg
-rw-rw-r--  0 0      0      319988 Apr 18 14:23 test96DPI.png
-rw-rw-r--  0 0      0      344286 Apr 18 14:25 test96DPI.tif

For password protected it shows contents but passphrase is only needed for extraction. It will not echo passphrase so if wrong try, try again.

C:\Programming>tar ftv "MyProtected=2a_Untitled.zip"
-rw-rw-r--  0 0      0        5467 Nov 30  1979 2a_Untitled.pdf

C:\Programming>tar -xf "MyProtected=2a_Untitled.zip"
Enter passphrase:
Enter passphrase:

In windows to write a zip without password or view its contents use

  • tar -acf filename.zip ..... to archive as zip format and
  • explorer filename.zip to see contents. You do not need the passphrase to see content, but you need the password to "see" the content, since that is NOT a password protected PDF, but as a PDF in a zip it can be a problem anyway. However that is a different story. (It may work for a single file.pdf but often not two !)

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36