Can I check the contents of encrypted zip-archive without using password?

1.1k Views Asked by At

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.

2

There are 2 best solutions below

8
Santiago Squarzon On BEST ANSWER

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

Note: An improved version of the function above is available as part of the PSCompression Module.

0
K J On

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