Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Directories in a ZipArchive C# .Net 4.5

A ZipArchive is a collection of ZipArchiveEntries, and adding/removing "Entries" works nicely. But it appears there is no notion of directories / nested "Archives". In theory, the class is decoupled from a file system, in that you can create the archive completely in a memory stream. But if you wish to add a directory structure within the archive, you must prefix the entry name with a path.

Question: How would you go about extending ZipArchive to create a better interface for creating and managing directories?

For example, the current method of adding a file to a directory is to create the entry with the directory path:

var entry = _archive.CreateEntry("directory/entryname"); 

whereas something along these lines seems nicer to me:

var directory = _archive.CreateDirectoryEntry("directory"); var entry = _directory.CreateEntry("entryname"); 
like image 564
Meirion Hughes Avatar asked Feb 28 '13 10:02

Meirion Hughes


People also ask

What are directories in C#?

A directory, also called a folder, is a location for storing files on your computer. In addition to files, a directory also stores other directories or shortcuts. In C# we can use Directory or DirectoryInfo to work with directories. Directory is a static class that provides static methods for working with directories.

How do I put files into a ZIP folder?

Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.


1 Answers

You can use something like the following, in other words, create the directory structure by hand:

using (var fs = new FileStream("1.zip", FileMode.Create)) using (var zip = new ZipArchive(fs, ZipArchiveMode.Create)) {     zip.CreateEntry("12/3/"); // just end with "/" } 
like image 155
Aimeast Avatar answered Oct 17 '22 06:10

Aimeast