Help checking contents of zip file

Hi all. I’m very new to powershell and i need your help with something.
I am looking for a way to check the contents of a list of zip files and if a specific file exists to replace it with a newer version.
Any idea how i can go about tackling this?

TIA

stathis95194,
Welcome to the forum. :wave:t3:

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

I have managed to list the filenames of the zip files. But if for example the file i am looking for is 1.txt and is listed in 2 zip files, how can i replace it with the newer version i have?

[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')

foreach($sourceFile in (Get-ChildItem -filter '*.zip'))
{
    [IO.Compression.ZipFile]::OpenRead($sourceFile.FullName).Entries.FullName |
        %{ "$sourcefile`:$_" }
}

How does the output look like? Is there a time stamp you can use to compare?

No, output looks like this:

PS C:\Program Files\7-Zip> .\test.ps1

GAC    Version        Location
---    -------        --------
True   v4.0.30319     C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a...
RS240808.ZIP:20240807.evs
RS240808.ZIP:20240807.rsp
RS240808.ZIP:20240807.suf
RS240808.ZIP:20240808.evs
RS240808.ZIP:20240808.rsp
RS240808.ZIP:20240808.suf
RS240808.ZIP:Class.ufd
RS240808.ZIP:PartOfSet.ufd
RS240808.ZIP:Position.ufd
RS240808.ZIP:UsedByHost.ufd
RS240811.ZIP:20240809.evs
RS240811.ZIP:20240809.rsp
RS240811.ZIP:20240809.suf
RS240811.ZIP:20240810.evs
RS240811.ZIP:20240810.rsp
RS240811.ZIP:20240810.suf
RS240811.ZIP:20240811.evs
RS240811.ZIP:20240811.rsp
RS240811.ZIP:20240811.suf
RS240811.ZIP:Class.ufd
RS240811.ZIP:PartOfSet.ufd
RS240811.ZIP:Position.ufd
RS240811.ZIP:UsedByHost.ufd

So if for example i am testing for “Class.ufd” i will need to replace it on both RS240808.ZIP and RS240811.ZIP

But you’d need to determine the version or date of the file before, don’t you?

How about using an external tool like 7zip?

Actually for simplicity let’s assume that the file i have is always newer and if it’s found inside a zip needs to be replaced. This is the part that i am not sure how to check. I can list the files inside the zip but how to check if that specific file is there AND replace it?

If the intent is to update any files in the archive you don’t even need to check. The Compress-Archive cmdlet has an -Update switch. It will check for newer files and update them for you.

2 Likes

Thanks for this. I had a look but unfortunately the -Update switch updates the archive even if the file is not present which is something i don’t need

Then you use a Where-Object to only output zip archives where the file is present. :man_shrugging:t3:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.