PoshCode Archive  Artifact [e14187783d]

Artifact e14187783d178570f0d560f2b3347c66662ef5c200bc209c2854984f420f5ed1:

  • File Script-to-compress-files-from-.ps1 — part of check-in [a259de2e2c] at 2018-06-10 13:36:41 on branch trunk — Script to compress files from command line. (user: Shadow size: 1416)

# encoding: ascii
# api: powershell
# title: 
# description: Script to compress files from command line.
# version: 0.1
# license: CC0
# x-poshcode-id: 411
# x-archived: 2012-06-30T05:24:07
# The script uses the windows shell zip manipulation methods to deal with compressed files.
#
# Name: zip.ps1
# Author: Shadow
# Created: 05/12/2008
param([string]$zipfilename=$(throw "ZIP archive name needed!"), 
      [string[]]$filetozip=$(throw "Supply the file(s) to compress (wildcards accepted)"))
trap [Exception] {break;}

#empyt zip file contents
$emptyzipfile=80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;

#create a new file with $ZIPFILENAME and copy the above array into it
$fmode = [System.IO.FileMode]::Create;
$zipfile = new-object -type System.IO.FileStream $zipfilename, $fmode;
$zipfile.Write($emptyzipfile,0,22);
$zipfile.Flush();
$zipfile.Close();

#open the new zip file as folder from Windows Shell Application
$shell=new-object -comobject Shell.Application;
$foldername=(ls $zipfile.Name).FullName;
$zipfolder=$shell.Namespace($foldername);

#Iterate  files to zip, copying them to zip folder.
#In the process, the shell compress them
Write-Host "Compressing:";
$filetozip| %{ls $_}| 
    foreach{ 
        ("`t"+$_.fullname);
        $zipfolder.copyhere($_.fullname,20);
        [System.Threading.Thread]::Sleep(250);
    }
$shell=$null;
$zipfolder=$null;