-3

MY circuit-board CAD package generates Gerber files with a non-standard filenames, all with the .GBR extension. I've been getting around this for many years by simply including a text file added within the .ZIP file that maps the filename to the layer name. However, this is becoming unworkable in this modern age of automated circuit board ordering.

All of the files from the CAD package are contained in a .ZIP file. In my old DOS days, I would use a batch file to create a temporary folder, extract the ZIP files into that folder, use a bunch of "if exist (filename.extension) ren (new-filename.new-extension)" statements, then create a new .ZIP file. Finally, over-write the original .ZIP file and delete the temporary directory.

I'm hoping that some bright person can tell me how I might go about doing all of that inside the original .ZIP file. I don't know if such a thing is even possible but I figured that this was a great place to find out.

If that is NOT possible, can someone give me advice on how to use PowerShell to accomplish this?

  • Question: which forum does this question belong with? I had assumed that this was the correct place to ask this question based on https://stackoverflow.com/questions/20485419/batch-file-script-to-zip-files/20486828 – Dwayne Reid Jun 18 '21 at 21:32

1 Answers1

3

This function renames nested files in a .zip file. Unfortunately you do have to extract and re-zip each file. If you have to rename only a portion of the files, this works great, otherwise it will be faster to extract the whole thing, rename, and repack:

Function Rename-ArchiveItem {
    param(
        $ZipPath,
        $ChildPath,
        $NewName,
        $TempFolder=$env:TEMP
    )

    # Load assembly, suppress output
    $null = Add-Type -AssemblyName 'System.IO.Compression.FileSystem'

    # Create a placeholder item for folder structure
    $tempfile = Join-Path $TempFolder $NewName
    $null = New-Item $tempfile -Force

    $zip = [IO.Compression.ZipFile]::Open( $ZipPath, 'Update' )
    
    # Extract the file and rename. Break to prevent deleting existing file.
    $entry = $zip.GetEntry( $ChildPath )
    If ($entry) { [IO.Compression.ZipFileExtensions]::ExtractToFile( $entry, $tempfile, $true ) }
    Else {throw "Child item not found";break}
    
    # Add renamed file
    Try {[IO.Compression.ZipFileExtensions]::CreateEntryFromFile( $zip, $TempFile, $NewName ) }
    Catch {"Error creating entry";$Error[0];break}
    
    # Cleanup
    $entry.Delete()
    $zip.Dispose()
    Remove-Item $tempfile
}

This example takes a file, renames it, and puts it into a different folder within the .zip file.

$ChildPath = 'readme.html' # top level is just file name
$ZipPath   = 'C:/temp/file.zip'
$NewName   = 'Folder1/readme.txt' # files in folders like so

Rename-ArchiveItem -ZipPath $ZipPath -ChildPath $ChildPath -NewName $NewName

I found 7zip has a function to let you rename items inside the zip file, but it seems to take about the same time to apply. It could be faster depending on the size of your archive and the files you're renaming.

Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16