Creating a .zip archive

D

Dennis

I found some code that was posted on codeguru.com for creating a .zip
file in VB. I want to do the same thing in VB.Net but it doesn't work.
Here is the original code:

Private Sub Command1_Click()
CreateEmptyZip "c:\testzip.zip"

With CreateObject("Shell.Application")
.NameSpace("c:\testzip.zip").CopyHere "c:\test.txt"
End With
End Sub


Public Sub CreateEmptyZip(sPath)
Dim strZIPHeader As String

strZIPHeader = Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18,
0)
With CreateObject("Scripting.FileSystemObject")
.CreateTextFile(sPath).Write strZIPHeader
End With
End Sub

I modified the last sub to work in VB.Net and that part seems to work
.... it does create an empty C:\testzip.zip file that opens fine in
winzip.

But the part that adds C:\test.txt to the archive fails with an error
(cannot create output file). C:\test.txt does exist.

Any idea what I am doing wrong?

TIA,
 
C

Cowboy \(Gregory A. Beamer\)

Download SharpZipLib instead:
http://www.icsharpcode.net/OpenSource/SharpZipLib/

It is very clena and works extremely well. It also has very good
documentation on how to use it. It is written in C# (the source), but you do
not have to edit the source (just use the DLL).

--
Gregory A. Beamer
MVP; MCP: +I, Se, SD, DBA

*************************************************
| Think outside the box! |
*************************************************
 
D

Dennis

I am not sure what you want to compress, but this is quiet standard.

http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

Well I took a look at the info on that page. Besides compression, I also
want to create an archive. There is a sample app at...

http://msdn.microsoft.com/en-us/library/ywf6dxhx.aspx

.... which does that. Unfortunately the archive it creates is, for lack
of a better word, non-standard. At the moment that should not be a
problem for me since my app will do all the reading and writing. I guess
it would be nice if the sample created an archive in a standard format,
but what do you want for free? ;-)

Thanks again,
 
T

Tom Shelton

Well I took a look at the info on that page. Besides compression, I also
want to create an archive. There is a sample app at...

http://msdn.microsoft.com/en-us/library/ywf6dxhx.aspx

... which does that. Unfortunately the archive it creates is, for lack
of a better word, non-standard. At the moment that should not be a
problem for me since my app will do all the reading and writing. I guess
it would be nice if the sample created an archive in a standard format,
but what do you want for free? ;-)

Thanks again,

Use SharpZiplib - seriously, the builtin System.IO.Compression stuff is crap
by comparison, and SharpZiplib is just as free :)
 
D

Dennis

Use SharpZiplib - seriously, the builtin System.IO.Compression stuff is crap
by comparison, and SharpZiplib is just as free :)

Can you point me to some vb.net code that uses SharpZiplib?
 
A

Andrew Morton

Dennis said:
Can you point me to some vb.net code that uses SharpZiplib?

This snippet is for creating zip files for a web user to download, hence the
HttpContext parts.
You should get some empirical data to determine a reasonable compression
level for the type of data you're putting into a zip archive, e.g. text
compresses well and quickly at higher levels. Jpeg files are pretty much
incompressible, so setting a high level would be a waste of time.

Imports System.IO
Imports ICSharpCode.SharpZipLib.Core
Imports ICSharpCode.SharpZipLib.Zip

Dim zipFileName As String = DateTime.Now.ToString("yyyyMMddThhmmss") &
".zip"
Dim zipFile As String =
Path.Combine(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
"downloads"), zipFileName)
Dim zos As ZipOutputStream = New ZipOutputStream(File.Create(zipFile))
Dim entry As ZipEntry
Dim fs As FileStream
Dim buffer(4095) As Byte
Dim actualFile As String

zos.SetLevel(2) ' use mild compression as we're packing in jpg files

' filelist is a List(Of String) of filenames; imgScrFolder is where they are
relative to the web root
For Each filename As String In filelist
actualFile =
Path.Combine(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
imgSrcFolder), filename)
' name the entry the filename w/o path
entry = New ZipEntry(filename)
' must set size to get WinXP zip decoder to work
entry.Size = New FileInfo(actualFile).Length
zos.PutNextEntry(entry)
fs = System.IO.File.OpenRead(actualFile)
StreamUtils.Copy(fs, zos, buffer)
fs.Close()
Next

zos.Close()

HTH
 
D

Dennis

This snippet is for creating zip files for a web user to download, hence the
HttpContext parts.

<snip code>

Playing around I came up with this to create a .zip ...

Imports ICSharpCode.SharpZipLib.Core
Imports ICSharpCode.SharpZipLib.Zip

Dim _fz As New FastZip
_fz.CreateZip(m_zipFile, m_sourceFolder, False, ".+\.txt$")

.... and this to extract a .zip ...

Dim _fz As New FastZip
_fz.ExtractZip(m_zipFile, m_sourceFolder, ".+\.txt$")

Thanks for your help!
 
C

Cheeso

DotNetZip has been updated.
DotNetZip is the managed-code library that allows .NET apps to create,
read, and manipulate ZIP archives.

v1.7.2.2 now supports WinZip's AES encryption "extension". With this
update, your apps can read and write AES-encrypted zip archives.


Other recent changes include:
- support for ZIP64
- support for zip2.0 (weak) encryption
- works on the .NET Framework (for PCs and servers) or .NET Compact
Framework (for devices)
- support for Unicode as well as arbitrary code pages
- create self extracting archives
- progress events for long-running operations
- embeds a managed-code port of Zlib


DotNetZip is really simple to use.
Example:

Using zip As ZipFile = New ZipFile
zip.AddFile("c:\photos\personal\7440-N49th.png", "")
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "")
zip.AddFile("ReadMe.txt")
zip.Save("test2.zip")
End Using

In contrast, the interface for SharpZipLib seems so complicated to
me.

DotNetZip is free, open source. The binary release requires a single
DLL at runtime. There's a full help file available.
Get the release at http://www.codeplex.com/DotNetZip/Release/ProjectReleases.aspx?ReleaseId=18985
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top