Tuesday, April 21, 2015

#56 : Compression with Powershell or Creating zip file with Powershell

File compression is not at all straight forward in Powershell. Using Windows compression is error-prone. It gives unexpected results and fails silently. The problem is basically with file size larger than 3 Gigs. If file-size is larger than 3 Gigs, Windows 2003 based server will not complete the task and fail silently. If you have delivered it for any critical task, you will find yourself in a real trouble.

Windows native compression method:

Below is the code which can be used : 

#------------------------------------------------------------------------------------------
#     Script : compress_file.ps1
#     Author : Som DT.
#    Purpose : Compress file script
#------------------------------------------------------------------------------------------

function compress-file ([string]$file, [string]$zipfilename)
{
 echo "Compressing [$file] to [$zipfilename]."
 
 if(-not (test-path($zipfilename)))
 {
  set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
  (dir $zipfilename).IsReadOnly = $false
  
  $shellApplication = new-object -com shell.application
  $zipPackage = $shellApplication.NameSpace($zipfilename)
  
  $zipPackage.CopyHere($file)
 
  #--Add some delay to wait -# 
  do {
   $zipCount = $zipPackage.Items().count
   echo "Waiting for compression to complete ..."
   Start-sleep -Seconds 1
  }
  while ($zippackage.Items().count -lt 1)
 
  echo "Finished zipping successfully"
 
 }
} 

#------------------------------------------------------------------------------------------

#--Starting comressing the file --# 
compress-file "D:\new.txt" "D:\new.zip"

#------------------------------------------------------------------------------------------




This works fine with Windows 2008 and above operating system. But still there are two problems -

1. CopyHere function is a real mess. It does not stick to the point where this function is called. It moves ahead, so you need to put a logic to make the script wait till compression is over.
2. This is hard to predict if compression failed. Script might run infinite in some cases. This is genuine problem and above script is not recommended.

6 comments:

  1. Hello

    I use 7zip, and just made some convenience functions in a powershell util. It is powerfull, and also support deep directory structures :-)

    Function Extract-Zip
    {
    param(
    [string]$zipfilename,
    [string]$destination
    )

    oCreateDir($destination)

    $shell_app=new-object -com shell.application
    $zip_file = $shell_app.namespace($zipfilename)
    $destinationDir = $shell_app.namespace($destination)
    $destinationDir.Copyhere($zip_file.items(), 0x14)
    }

    Function Extract-7Zip
    {
    param(
    [string]$zipfilename,
    [string]$destination
    )

    $cmdOptions = "x $zipfilename -o$destination -y"
    $ret = C:\pathto\7zip\7za.exe $cmdOptions

    $cmd = "C:\pathto\7zip\7za.exe x $zipfilename -o$destination -y"
    $ret = cmd /c $cmd

    return $ret
    }

    ReplyDelete
  2. Hi Marcus,

    7-zip is fairly simple to use.
    The only problem is - 7-zip is not found by default in all organizations as it is third-party tool.

    There is one more third-party method which I will publish soon.

    Thanks Marcus for the reply and sharing the code!

    regards,
    Som Dutt Tripathi

    ReplyDelete
  3. Hi Som,

    If you have similar example (#56 : Compression with Powershell or Creating zip file with Powershell) to zip folders and files both. Pls share.

    Thanks
    Santosh

    ReplyDelete
  4. Are you trying to make cash from your visitors by using popup advertisments?
    If so, did you try using Clickadu?

    ReplyDelete
  5. So as to make sure that you correctly edit a PDF doc, it's best to hold a couple of issues in thoughts. First is to put a file below the scanner as straight as potential. Then you may select to press the scan button on the scanner entrance and choose "purchase picture" possibility. If you want to learn more about this topic please visit onlineconvertfree

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...