Copy Files from ZIP archives

Welcome Forums General PowerShell Q&A Copy Files from ZIP archives

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 8 months ago.

  • Author
    Posts
  • #24484

    Participant
    Points: 0
    Rank: Member

    I have a script to copy files from one location to another based on an input file.

    The input file is in the format:

    C:\Store\firstfile.txt
    C:\Store\secondfile.txt
    etc.

    This all works fine, but the input file also contains references to a file within a zip file in the format:

    C:\Store\Logs\mylog.zip\filetobecopied.txt

    Where filetobecopied.txt is inside mylog.zip. The zip file may also contain a folder structure, e.g

    C:\Store\Logs\mylog.zip\Reports\filetobecopied.txt.

    I've tried the following so far,, but no success (powershell v3)

    get-content $sourceFile | foreach-object {

    $FileToCopy = $_
    if ($FileToCopy.Contains(".zip")) {

    $FileToCopy = $FileToCopy.Substring(0, $s.IndexOf('.zip')) + '.zip'
    }
    $ObjSourceFile = get-item $FileToCopy

    $SourceFileName = $ObjSourceFile.name
    $SourceDirectory = $objSourceFile.DirectoryName

    Is there a way of achieving this?

  • #24491

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    You can't read a file directly from a ZIP; you would need to first expand the ZIP.

  • #24514

    Participant
    Points: 32
    Rank: Member

    You can do this with a Shell.Application COM object.
    My test file robots.zip contains a file called robots.txt

    
    $shellApp = New-Object -ComObject Shell.Application
    $destination = 'F:\' 
    $fileToCopy = 'F:\__Temp\robots.zip\robots.txt'
    $shellApp.NameSpace($destination).copyhere($fileToCopy)
    
    
  • #24531

    Participant
    Points: 0
    Rank: Member

    Thanks Matt, that was exactly what I was looking for.

The topic ‘Copy Files from ZIP archives’ is closed to new replies.