Sharpziplib compress file example
OpenRead source , File. Decompress File. Length]; inStream. Read buffer, 0, buffer. PutNextEntry entry ; zipOutStream. Getting Support. SharpZipLib in a Nutshell SharpZipLib gives developers great flexibility and power in creating archives in standard, well-known formats. Get it now. Version covered. Home page. Introduction This article shall describe an approach that may be used to collect files from multiple locations and zip them up into a single zipped folder.
The code contained in the sample project may be useful, if one needs to gather up multiple files, zip them up, and then do something with them, such as upload the zipped files to a server location for storage or processing. The application uses the SharpZipLib for the basis of the compression function. The Solution The solution contains a single project. The example is provided in the form of a single Windows Forms project; the project contains a single main form frmMain ; the references are all in the default configuration, with the exception being that the SharpZipLib DLL has been added.
Having downloaded the DLL from the www. All of the code necessary to drive the application is included in the main form's code file. All UI and code required by the project are contained in the single main form frmMain. The only references added to the project were those necessary to support the use of the SharpZipLib in the project, System. IO, and System. The imports and class declaration are as follows:.
Imports ICSharpCode. Imports System. Public Class frmMain. The next block of code is used to handle the browse button's click event. This button is used to display the open file dialog; this dialog is used to capture the path to a folder that the user wants to include in the zipped folder. Object, ByVal e As System. EventArgs Handles btnBrowse. If openFileDialog1. Cancel Then. End If. Dim sFilePath As String.
If System. End Sub. The next block of code is used to add a file selected by the previous method Browse button and add to a list of files to included in the zipped folder. Again, this section of code is annotated to describe what each part of the code does. EventArgs Handles btnAddFile. If txtAddFile. Empty Then. Dim i As Integer. Count - 1. If lstFilePaths. Items i. ToString Then.
The application uses the SharpZipLib for the basis of the compression function. The solution contains a single project.
The example is provided in the form of a single Windows Forms project that contains a single main form frmMain. The references are all in the default configuration, with the exception being that the SharpZipLib DLL has been added first item in the reference list. The Compress Folders project is a Windows Forms project containing a single form.
All UI and code required by the project are contained in the single main form frmMain. The only references added to the project, aside from the defaults, were those necessary to support the use of the SharpZipLib in the project. The imports, namespace and class declarations are as follows:. After the class declaration, the next block of code is the default constructor and empty form load event handler:. This button is used to display the open file dialog, which is used to capture the path to a folder that the user wants to include in the zipped folder.
The code is annotated such that you may review descriptions of what each section of the code is doing by reading though this code block:. The next block of code is used to add a file selected by the previous method Browse button and add to a list of files to include in the zipped folder. Again, this section of code is annotated to describe what each part of the code does. This method allows the user to remove items from the listbox list after they have been added using the Browse button and Add button.
Next up is a button event handler that uses the Folder Browser Dialog control to help the user specify a destination path for the finished ZIP file. The next button click event handler contains the code used to gather up the marked files and zip them up the destination folder set.
Exists outFileInfo. CreateDirectory outFileInfo. ZipEntry inFileInfo. Name ; newEntry. UtcNow; zipStream. CloseEntry ; zipStream. MrEyes MrEyes 12k 9 9 gold badges 45 45 silver badges 66 66 bronze badges. As to your aside: ZipStream wouldn't make much sense, since ZIP is an archive format that can hold more than one file. As an aside of my own, as you're only compressing a single file, is there a reason why you don't want to just use GZip compression, which, as you note, has support within the framework?
I would love to, I could do this with a couple of lines of code. It has to be zip as our 'support' desk can't handle the complexities of gzip did that sound sarcastic?? Add a comment. Active Oldest Votes. Hans Passant Hans Passant k gold badges silver badges bronze badges.
0コメント