Solved

Zip and unzip a file from a VB.NET application

Posted on 2004-10-08
4
1,697 Views
Last Modified: 2012-08-13
Hi,

I have an empty zipped Access database in the Folder where my VB.NET application is located. Thsi database serves to create other databases by copying it to the required folders.

I'd like the application to copy it to a specific folder and unzip it there so that the user can store his data.

My problem is how to unzip the file.


On the other hand I want to be able to zip files and folders for backup purposes.


In this case my problem is how zip the files from within my VB.NET app.

Any help shall be greatly appreciated

0
Comment
Question by:Ghanisen
  • 2
4 Comments
 
LVL 6

Expert Comment

by:danths
Comment Utility
To zip and unzip you would need third party libraries ( you can write from scratch as well but no point).
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

The above link has very good examples and the library it self is opensource with no tags attached. I have used this on multiple occasions and works great. You could laso called native binaries like pkzip but a library integrates much better than out of process model.
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
Comment Utility
You can use WInZip
// Assumes:Need a legal copy of WinZip

    Public Function RunExe(ByVal strCommandLine As String)
    '**************************************************************
    Dim objShell As System.Object
    Try
    objShell = CreateObject("WScript.Shell")
    objShell.run(strCommandLine)
    objShell = Nothing
    Catch err As Exception
    'log the exception
    EventLog.WriteEntry(source:=strClassName & ": RunExe", _
    message:=err.Message & vbCrLf & "Command Executed: " & strCommandLine, _
    Type:=EventLogEntryType.Error)
    Finally
    End Try
    End Function
    Public Function ZipUpFile(ByVal strZipFile As String, ByVal strFileNames As String)
    '**************************************************************
    ' strZipFile: zipfile is the name of the Zip file you want to work with. You may specify a drive and/or folder.
    ' If you don't provide a filename extension, WZZIP adds ".zip". You can specify a different extension, but
'doing so may prevent WinZip® and other Windows Zip products from operating correctly with the Zip file.
'If no Zip file exists with the name you specify, WZZIP will create the file.
    ' strFileNames: Filenames may also include wildcards. The wildcard specification "*" is assumed to mean
 "*.*", i.e., all files. The wildcard specification "*." means "all files that have no extension."
    ' examples:
    ' wzzip test.zip *.*
    'Adds all files in the current folder to the Zip file Test.zip.
    ' wzzip(test.zip * .txt)
    'Adds all files with a TXT extension to the Zip file Test.zip.
    '**************************************************************
    Dim strFileName As String = "ZipUpFile"
    Dim strCommand As String
    Try
    strCommand = "wzzip " & """" & strZipFile & """" & " " & """" & strFileNames & """"
    RunExe(strCommand)
    Catch err As Exception
    'log the exception
    EventLog.WriteEntry(source:=strClassName & ":" & strFileName, _
    message:=err.Message & vbCrLf & "Arg in: " & vbCrLf & "strFileName: " & strFileName, _
    Type:=EventLogEntryType.Error)
    Return False
    Finally
    End Try
    End Function

    Public Function ZipUpFile(ByVal strZipFile As String, ByVal strFileNames As String, ByVal strParameters As String)
    '**************************************************************
    ' inputs:
    ' strZipFile: zipfile is the name of the Zip file you want to work with. You may specify a drive and/or folder.
    ' If you don't provide a filename extension, WZZIP adds ".zip". You can specify a different extension,
 but doing so may prevent WinZip® and other Windows Zip products from operating correctly with the Zip
file. If no Zip file exists with the name you specify, WZZIP will create the file.
    ' strFileNames: Filenames may also include wildcards. The wildcard specification "*" is assumed to mean
"*.*", i.e., all files. The wildcard specification "*." means "all files that have no extension."
    ' examples:
    ' wzzip test.zip *.*
    'Adds all files in the current folder to the Zip file Test.zip.
    ' wzzip(test.zip * .txt)
    'Adds all files with a TXT extension to the Zip file Test.zip.
    ' zip file parameters are in comments below this function
    'an example is
    'wzzip -xABC.TXT test.zip *.txt
    'Adds all files with a TXT extension to Test.zip except ABC.TXT.
    '**************************************************************
    Dim strFileName As String = "ZipUpFile"
    Dim strCommand As String
    Try
    strCommand = "wzzip " & strParameters & """" & strZipFile & """" & " " & """" & strFileNames & """"
    RunExe(strCommand)
    Catch err As Exception
    'log the exception
    EventLog.WriteEntry(source:=strClassName & ":" & strFileName, _
    message:=err.Message & vbCrLf & "Arg in: " & vbCrLf & "strFileName: " & strFileName, _
    Type:=EventLogEntryType.Error)
    Return False
    Finally
    End Try
    End Function
    ' zip file parameters
    ' The options are:
    '-a      Add files (default action).
    'Related option
    '-a+      Remove the archive attribute from each file after adding it to the Zip file. As WinZip® adds a file
during the zipping process, the file's archive attribute is reset.
    'Related options
    '-b[drive|path]      Use another drive for the temporary zip file. Since WZZIP creates a new, temporary zip
file when it updates a file, sufficient space must be available on the current drive for that file. This option
enables you to use another drive for that purpose when space considerations force the issue.
    '-c      Create or edit existing comments for all the files in the Zip file.
    'Related options
    '-C      Create comments only for files being added that are not already in the Zip file.
    'Related options
    '-d      Delete specified file(s) from the Zip file.
    '-e<x|n|f|s|0>      Set the compression level. -ex = maximum (smallest file); -en = normal; -ef = fast;
-es = super speed; -e0 = no compression
    '-f      Freshen: replace files that are already part of the Zip file and are newer on disk.
    'Related option
    '-h|-?      Show this help file
    '-i[-]      Add files whose archive attribute is set. By default the archive attribute is then removed. Use the
optional "-" suffix to leave the archive attribute on.
    'Related options
    '-jhrs      Do not store hidden, read only, and system attributes in the Zip file. (-whs controls whether
hidden and system files are added.)
    'Related options
    '-Jhrs      Store hidden, read only, and system attributes in the Zip file (default). (-whs controls whether
hidden and system files are added.)
    'Related options
    '-k      Do not update the Zip file's file date; retain the original date.
    'Related option
    '-m[f|u]      Move files to the Zip file. When the Add|Freshen|Update operation is complete, the
original files are deleted.
    'Related option
    '-o      Change the Zip file's file date to the same as the newest file in the Zip file.
    'Related option
    '-p|P      Store folder names. A lower case p stores only the names of folders recursed (subfolders included)
 via the -r option, while an upper case P stores all folder information specified on the command line.
    'Related option
    '-q      Display ANSI control codes within comments (ansi.sys must be present). This option affects
display only; it does not strip ANSI codes while creating a comment. Note: ansi.sys is not supported under
Windows NT 4.0.
    '-r      Recurse into subfolders (include subfolders). This option requires the -p or -P option.
    'Related option
    '-s[password] Specify a password (if no password is given, a prompt will be generated). Passwords
containing spaces must be enclosed in double quotes.
    '-t[f][date]      Include files with a date equal to or more recent than the date specified (current date
if no date specified).
    'Date formats without the f modifier depend on country settings; examples with and without delimiters:
    '      United States: mmdd[yy]yy or [m]m/[d]d/[yy]yy
    '      Japan: [yy]yymmdd or [yy]yy/[m]m/[d]d
    '      Most of Europe: ddmm[yy]yy or [d]d.[m]m.[yy]yy
    '      valid delimiters are / . or -
    'The f modifier indicates that the date is specified in either of the following country-independent formats,
regardless of the system's locale settings:
    '      [yy]yymmdd
    '      [yy]yy-[m]m-[d]d
    'Note that years can be specified as two or four digits. Two digit years less than 80 are assumed to be
20##, while two digit dates greater than or equal to 80 are assumed to be 19##.
    'Related option
    '-T[f][date]      Include files older than the date specified (current date if no date specified)
    'Date formats without the f modifier depend on country settings; examples with and without delimiters:
    '      United States: mmdd[yy]yy or [m]m/[d]d/[yy]yy
    '      Japan: [yy]yymmdd or [yy]yy/[m]m/[d]d
    '      Most of Europe: ddmm[yy]yy or [d]d.[m]m.[yy]yy
    '      valid delimiters are / . or -
    'The f modifier indicates that the date is specified in either of the following country-independent formats,
 regardless of the system's locale settings:
    '      [yy]yymmdd
    '      [yy]yy-[m]m-[d]d
    'Note that years can be specified as two or four digits. Two digit years less than 80 are assumed to be
20##, while two digit dates greater than or equal to 80 are assumed to be 19##.
    'Related option
    '-u      Update: add files that are new or have changed. This command adds to the Zip file any files that
are not already in the Zip file, and replaces any files that have a more recent date on disk. Put another way, this command is the same as -a (Add) except that it skips files that are already in the Zip file and have the same date in the Zip file and on disk..
    'Related option
    '-v[b|t][r][m][c][d|e|n|o|p|s][f] View the list of files in the Zip file in one of the following formats:
normal, brief, or technical. r reverses the sort order, m (more) pauses after each screen, and c displays any
file comment. You can optionally sort by date, extension (file type), name, original order, percentage
compression, or uncompressed size. The f option causes the date to be displayed in the format
yyyy-mm-dd regardless of the local system's display settings.
    'Related options
    '-vi[m]      View the Zip file's internal information. Use the optional "m" suffix to prevent text from
scrolling off the screen.
    'Related options
    '-whs      Include hidden and system files in the zipping process.
    'Related options
    '-Whs      Exclude hidden and system files from the zipping process (default).
    'Related options
    '-x<filename> Exclude the specified file(s) from the zipping process. Wildcards are allowed.
    'Related options
    '-x@listfile      Exclude the files listed in the specified listfile from the zipping process. This listfile uses
the same format as the listfile used to specify files to be zipped (see WZZip Usage).
    'Related options
    '-yb[c]      Automatic, non-interactive ("batch" mode) handling of prompts. If a prompt is issued,
the operation terminate with error level 250. Use the optional c suffix to automatically continue with a "yes",
"ok" response instead of terminating.
    '-yk      Force the use of MS-DOS (8+3) files names within the Zip file.
    '-yp      Display a "Press any key to continue" prompt when the operation completes. This is particularly
useful when running WZZIP from Start Menu's Run dialog box or from the "start" command.
    '-z      Create or edit a Zip file comment.
    'Related options
    '-&[w]      Span to multiple removable disks. Use the optional w suffix to wipe out all files on the removable disk.
    ' [s[drive]]      Use the optional s suffix to zip up entire disk with subfolders.
    '(Windows NT must use "-&" or -^& due to cmd.exe requirements)
    '-$[drive]      Save volume label as a part of the Zip file; the current drive is used if not otherwise
specified.
    '-@list      Create a file listing all the files that would be archived if this option were not specified. Hint:
remember to use the leading dash to create a list. Leave out the leading dash to use a list of files in a
WZUNZIP command.
    'Related options
    '-^      Display the command line on your screen
    '(Windows NT users must specify "-^" or -^^ due to cmd.exe requirements)

0
 

Expert Comment

by:SubSonicS
Comment Utility
Hi, i know this post is closed, but i have a better way...i'm using it within my apps...

Simple use the FREE Expand.exe within Microsoft Windows...
You can find an easy way to execute it within your application....

It is very stable and it will compress your files fast and totally secure...

To be Know: The version of expand.exe of Win98/me is different from the one shipped with Nt/XP/2003...

I simple used all two inside my app and i use the appropriate one after a check of the installed OS..

Hope this help to be more FREE then use WinZip(A little question, how can you know if WinZip will be installed on the user machines?)

Sorry for my bad English, i'm Italian...
0
 

Expert Comment

by:SubSonicS
Comment Utility
P.S. Naturally expand.exe does not create "Zip" files, but "Cab" files which have the same properties of compression....

;)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now