::::::::: PowerShell :::::::::
Monday, February 27, 2006
  [MSH] Displaying Zip File contents
Let's have some fun with zip files.

I have searched through MSDN document on whether .NET library but could not finddd
any C#/VB.net library that deals with Zip file content.

I have been googling around the web but found a useful J# library for dealing with
zip files easily. The name of J# library is called "VJSLIB.DLL".

Here is the step on how to get a Zip file content

  1. Open "java.io.FileInputStream" for a zip file

  2. Pipe File Input stream object to "java.io.ZipInputStream"

  3. While there exists a content in a zip input stream, get a content of type "ZipEntry"

  4. Close "ZipInputStream" first and then "FileInputStream"



# Author: DBMwS
# Date: 02/27/2006 @ 04:13 PM EST
# Description: Retrieve a Zip file's content
# (either display only files inside the zip file or return ZipEntry object)
# Note: This function requires J# Library, "vjslib"
# Usage: get-ZipContent file_name [[System.Bool] -toString]
# Reference: http://codeproject.com/csharp/VmEasyZipUnZip.asp
# Params
# 1) $zipFileName: Path of zip file
# 2) $toString: return name of zip content, otherwise return ZipEntry
function Get-ZipContent {
param($zipFileName = $(throw "Enter Zip File Name"), [bool]$toString = $true)

# Load J# library
# used "out-null" instead of "[void]" since "out-null" would not display
# loaded assembly even if there was an exception thrown
[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

# 1) Open "java.io.FileInputStream" for a zip file
$fis = new-object java.io.FileInputStream($zipFileName)
# 2) Pipe File Input stream object to "java.io.ZipInputStream"
$zis = new-object java.util.zip.ZipInputStream($fis)

# 3) While there exists a content in a zip input stream, get a content of type "ZipEntry"
while (($ze = $zis.getNextEntry()) -ne $null) {
if ($toString) {
# Pass string value to pipe
Write-Object $ze.ToString()
} else {
# Pass actualy ZipEntry object to pipe
$ze
}
}

# 4) Close "ZipInputStream" first and then "FileInputStream"
$zis.close()
$fis.close()

trap {
if ($zis -ne $null) { $zis.close() }
if ($fis -ne $null) { $fis.close() }
break
}
}
It is still not complete since, it does not list a zip file inside a given zip file name(no recursive listing)
But I have left that part out on purpose since "get-ZipContent" can be called recursively in a while loop in another function.

Here is how one can utilize "Get-ZipContent"

MSH>ls | where {!$_.MshIsContainer -and $_.name -like "*.zip"} | foreach { get-zipcontent $_.fullname } | where { $_ -like "*.jpg" }


In above code snippet, i have used "where {!$_.MshIsContainer -and $_.name -like "*.zip"}" to filter out directories and select files with only "*.zip" extension
(Well, if you don't filter out files that way, get-ZipContent would fail half way when a directory or a non-zip file has been passed to it)

Well, I will be talking about how to actually "extract" the contents in the next blog.

EDIT: Tested under both Monad v3.0 & v3.1 ;) Refer to /\/\o\/\/'s blog on Beta v3.1 release here



Reference: Zip and Unzip from a C# program using J# runtime by Valeri

Tags :
 
Comments:
I think actually you're looking for the new GZipStream class in System.IO.Compression :-)
 
Sweet.
When I checked out WinFx Document, i was able to find that namespace.
Thanks for the tips there, to both Nick and "Anonymous" poster..
 
i use the jar command line from jdk:
jar tvf myfile.zip
jar tvf myfile.jar
 
My experience advises in this condition use next tool-repair broken zip file,because it has many pluses,such as program is free as far as I can see,has many resources,and it can save a lot of free space on your HDD, when applied to files, that are used rarely,program is so easy to use, that it does not require any technical skills, anyone, who knows Windows interface, can work with this tool,supports Windows 98, Windows Me, Windows NT 4.0, Windows 2000, Windows XP, Windows XP SP2, Windows 2003 and Windows Vista,program is very powerful, it contains several different algorithms, developed in our company, for accurate data recovery,repair zip file broken and repair broken zip file does not modify source file during the process, so, you can take it and try to recover with any other Zip repair service and compare the results.
 
My sister told me about her problem,and I advised-recover broken zip files.This tool helped me and friends too not once,also program is free as far as I know.It can recover corrupted files in Zip format and compatible with all supported versions of Windows operating system.
 
I often join my zip files and once I did it and both files were deleted!I didn't know what to do.But I used next tool-best recovery zip software,which I found in one forum and tool solved my problem.Moreover it is free and can too extract data from corrupted files with *.zip extension.
 
Post a Comment



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger