Unpacking subdirectories and files in C #

I am trying to implement the unpack function in a project that I am currently working on, but the problem is that I have some limitations when it comes to licensing, and I need to stay away from GPL licenses because the project is closed.

So I can no longer use SharpZipLib .. so I switched to the .Net libraries I'm currently trying to work with the ZipArchive library.

The problem is that it is not extracted for directories / subdirectories, so if I have blabla.zip that has file.txt inside and / folder / file 2.txt it will all be extracted to file.txt and file2.txt so it ignores the subdirectory.

I am using an example from the MSDN website. which looks something like this:

using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { foreach (ZipArchiveEntry entry in archive.Entries) { entry.ExtractToFile(Path.Combine(extractPath, entry.FullName)); } } 

Any idea how to solve this?

+6
source share
1 answer

if your archive looks like this:

 archive.zip file.txt someFolder file2.txt 

then entry.FullName for file2.txt someFolder/file2.txt , so even your code will work fine if the folder exists. Therefore, you can create it.

 foreach (ZipArchiveEntry entry in archive.Entries) { string fullPath = Path.Combine(extractPath, entry.FullName); if (String.IsNullOrEmpty(entry.Name)) { Directory.CreateDirectory(fullPath); } else { if (!entry.Name.Equals("please dont extract me.txt")) { entry.ExtractToFile(fullPath); } } } 

or you should use the static ZipFile method if you need to extract the whole archive

 ZipFile.ExtractToDirectory(zipPath, extractPath); 
+14
source

Source: https://habr.com/ru/post/953545/


All Articles