0

I'm trying to use ExtractToDirectory function and it's working fine when there is no file exist. (Unzipping is ok when there is not same file name) After checking microsoft site and looking at ExtractToDirectory I found there are more 2 overload functions that supposed to shown.

All I can use is: (source,path) and (source,path,encoding) no boolean is found (that supposed to show if overwrite is allowed)

James Z
  • 12,209
  • 10
  • 24
  • 44
UniqueMU
  • 19
  • 6
  • Is that you need ? [System.IO.Compression.ZipFile.ExtractToDirectory(String, String, Boolean)](https://learn.microsoft.com/dotnet/api/system.io.compression.zipfile.extracttodirectory#System_IO_Compression_ZipFile_ExtractToDirectory_System_String_System_String_System_Boolean_) –  Sep 21 '20 at 15:56
  • Are you trying to extract to a folder or an existing zip file? I think you are trying to extract to a ZIP file and not a folder. Enable the extension in you file explorer so you know the differences between a folder and a zip file. – jdweng Sep 21 '20 at 16:41
  • @OlivierRogier yes, somehow I can't use this function (only string, string, encoding) I need to use (string,string,bool) but the function doesn't exist. – UniqueMU Sep 22 '20 at 15:02

1 Answers1

0

I see that the method System.IO.Compression.ZipFile.ExtractToDirectory(String, String, Boolean) is only available since .NET Core 2.0, not .NET Framework nor .NET Standard.

It will be available on .NET 5.

So on .NET Framework you need to delete files of an existing folder before extracting... or use another assembly from another developer:

using System.IO;
using System.IO.Compression;

if ( Directory.Exists(extractToPath) )
  // Ask user confirmation if needed
  Directory.Delete(extractToPath, true);

ZipFile.ExtractToDirectory(zipSourcePath, extractToPath);

System.IO.Directory.Delete()

Don't forget to add the reference in the project to the assembly System.IO.Compression.FileSystem (see images below).

To only update the destination folder

With new files in the zip, you can extract the zip in a subfolder in the user temp folder, and next, move this folder to the folder where you have your tree to update.

It should do what you need.

string tempPath = System.IO.Path.GetTempPath();

ZipFile.ExtractToDirectory(zipSourcePath, tempPath);

And next use:

Directory.Move doesn't work (file already exist)

MoveDirectory(tempPath, extractToPath);

Because Directory.Move(string, string) Method can't be used since it does not allow overwriting..

Don't hesitate to open a new question if you have difficulty doing that.


enter image description here

enter image description here

enter image description here

enter image description here

  • I thought about this idea (to OpenRead and then delete file if exist and unzip) I'm not that strong in C# so I would like to get your help by helping me coding these lines with this function. Thanks a lot – UniqueMU Sep 22 '20 at 15:37
  • Thanks. one more question: if ( Directory.Exists(extractToPath) ) -> Checks file(s) name inside ZIP? After all I need the function to only *update* new files that are from ZIP For example: inside UPDATE.ZIP there is a text file named: Update.txt and in @"." (root) there is a file called the same (Update.txt) I need the file to be deleted and then unzip the new Update.txt if it works- will this code will work if I need to update specific file in directory? For example: Update\Update.txt will it keep the older files but replace only the new? or will delete the whole folder "Update"? – UniqueMU Sep 22 '20 at 16:03
  • If you only need to extract some files to update an existing file system tree, I don't know. Perhaps using another component like https://github.com/haf/DotNetZip.Semverd. Else you can extract the zip in a folder in the user temp folder and next merge it in the destination system tree by moving files –  Sep 22 '20 at 16:05
  • Both ideas sound very smart but I really don't know how to add a component or how to merge files.. I thought this function will be so easy because of the overwrite and then it became very complicated.. Maybe you can upload something quick here or contact me at "Discord"/"Skype" so we can chat? Thanks a lot again, you're really helpful. – UniqueMU Sep 22 '20 at 16:18
  • I wondered if there's a way to use ZipFile.OpenRead to run all over the zip and then use your code and check if specific file is exist if yes delete if no extract. – UniqueMU Sep 23 '20 at 17:19
  • I succeed to make it work to extract files (if exist delete and extract) Now I'm having another problem caused by folders inside the zip file. Example: Update.zip includes Data folder and inside there are files to update How can I Open the zip check if folder exist and if it exist- save the path and do the same thing until you get to files. (only update the files to direct path inside the zip) Example: update.zip Data/Text.txt should be extracted to Data/Text.txt Example 2: update.zip Data/Folder/Text.txt should be extracted to Data/Folder/Text.txt Thanks! – UniqueMU Sep 24 '20 at 12:00
  • I don't know, I never used ZipFile and I see that available methods are only Create, Extract, Open and OpenRead... But there is [ZipArchive.Entries](https://learn.microsoft.com/dotnet/api/system.io.compression.ziparchive.entries). You should be able to parse the content to find what you need. –  Sep 24 '20 at 13:15
  • I'm trying to use ZipArchive.Entries but I still can't understand how I check if entry is folder – UniqueMU Sep 25 '20 at 03:57
  • Didn't the example on the Microsoft page help you navigate the entries (`entry.FullName`) ? Also you can apply Linq extension methods like `Where`, `Any` and so on. –  Sep 25 '20 at 11:09