C sharp zip directory


















The second parameter "entryName" in CreateEntryFromFile should be a relative path, and when unpacking the zip-file the directories of the relative paths will be detected and created. There is a ZipPackage class in the System. Packaging namespace which is built into. NET 3, 3. Here is an example how to use it. There's an article over on MSDN that has a sample application for zipping and unzipping files and folders purely in C. I've been using some of the classes in that successfully for a long time.

The code is released under the Microsoft Permissive License, if you need to know that sort of thing. Based on my experience of a previous version of this I'd happily recommend it. SharpZipLib is also quite a mature library and is highly rated by people, and is available under the GPL license. It really depends on your zipping needs and how you view the license terms for each of them. Following code uses a third-party ZIP component from Rebex :. You can download the ZIP component here. You need to add the dll file as a reference in your project.

ComponentPro ZIP can help you achieve that task. The following code snippet compress files and dirs in a folder. You can use wilcard mask as well. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Zip folder in C Ask Question. Asked 12 years, 7 months ago. Active 1 month ago. Viewed k times. What is an example simple code of how to zip a folder in C? OK, but I need next information.

Improve this question. Peter Mortensen In my sample, you have a simple Web Application with a button and a label to display the result of this process. When you press the button, a mix of C and J is called to zip a folder. NET compiles the project, it will assemble this mix as one single application. Notice that you must have J libraries installer on your Web server, otherwise you won't be able to have this working.

The most interesting point about this solution is that you can use both C and J libraries on the same code, compiling them together in order to complete a solution. C for itself, with its standard libraries, does not provide zip methods. I want to show one piece of user interface code because it highlights an important issue when removing items from a collection.

As long as the list has selected files, the program removes the first of them. When it removes the first selected file file, the indices of any files that come after it in the list decrease by 1. The SelectedIndices list updates so it holds the new indices of the remaining items, and the process can continue. The loop continues removing the first selected item from the list until there are no more selected items.

Another approach that you might try would be to make a variable, say i , loop through the indices of the selected items and remove them from the list. Unfortunately when you do that, the indices update and the variable i increments so it skips entries. For example, the first time through the loop, the code removes the first selected index. Now the item that was second becomes the first with index 0 , but i increments to 1 so it is past the new first item. You also cannot use a foreach loop to iterate through the SelectedItems or SelectedIndices list and remove the items in those lists.

Whenever you remove an item, that modifies the lists and that messes up the foreach loop. The code shows a few other strategies that work commented out. If you study them, you should be able to figure out how they work. When you add files to a zip archive, the file names within the archive are relative.

That way when you unzip a directory hierarchy, the files go where they belong. To solve that problem, the example appends a number to files with the same names. For example, if you select three files named myfile. This code displays a SaveFileDialog to let you select the soon-to-be-created zip file. If the directory is empty, an empty archive is created.

This method overload does not include the base directory in the archive and does not allow you to specify a compression level. If you want to include the base directory or specify a compression level, call the CreateFromDirectory String, String, CompressionLevel, Boolean method overload. If the archive already exists, an IOException exception is thrown. If an entry with the specified name already exists in the archive, a second entry is created with an identical name.

If a file in the directory cannot be added to the archive, the archive is left incomplete and invalid, and the method throws an IOException exception. One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

When compressing the archive, the base directory is included and the compression level is set to emphasize the speed of the operation over efficiency. Use this method overload to specify the compression level and whether to include the base directory in the archive.

The encoding to use when reading or writing entry names in this archive. Specify a value for this parameter only when an encoding is required for interoperability with zip archive tools and libraries that do not support UTF-8 encoding for entry names.



0コメント

  • 1000 / 1000