Decompressing password-protected ZIP files with .NET 4.5 - c#

Microsoft introduces improvements for ZIP file handling in .NET 4.5 in the System.IO.Compression namespace. Namely the classes ZipArchive and ZipFile.
However, I have not yet seen a way to use native .NET ZIP file handling for password protected files. Is there a way to achieve this? (I am aware that there are pretty good 3rd party zip file libraries, that is not the question.)

Unfortunately not. There is no support within the .Net Framework 4.5 for password protected zip files. In this case you have to switch to one of the well known 3rd party libraries.

As pointed out, DotNetZip is your friend. Unpacking your zip file is as easy as
using ( ZipFile archive = new ZipFile( #"c:\path\to\your\password\protected\archive.zip",) )
{
archive.Password = "your-pass-word-here" ;
archive.Encryption = EncryptionAlgorithm.PkzipWeak ; // the default: you might need to select the proper value here
archive.StatusMessageTextWriter = Console.Out;
archive.ExtractAll( #"c:\path\to\unzip\directory\", ExtractExistingFileAction.Throw ) ;
}
In my experience, DotNetZip runs about as fast as Info-Zip's open source unzip utility and uses roughly the same amount of memory.
Edited To Note: DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still available at Codeplex. It looks like the code has migrated to Github:
https://github.com/DinoChiesa/DotNetZip. Looks to be the original author's repo.
https://github.com/haf/DotNetZip.Semverd. This looks to be the currently maintained version. It's also packaged up an available via Nuget at https://www.nuget.org/packages/DotNetZip/

The ionic method is awesome. I tried three other approaches, and it is by far the best. Don't waste time, just use it.
https://dotnetzip.codeplex.com/wikipage?title=PS-Examples
Supports password encrypted, and other zip options.

In looking at the methods provided by the 4.5 framework there is not a method that allows passwords with zip files. As mentioned in your question 3rd party is going to be your best bet.

There does not appear to be any support for password protected zip files in the native .net 4.5 library, similar to how there does not appear to be support in windows explorer, even with Windows 10!
Some people have reported that they have zip corruption issues using the 3rd party DotNetLib, so make sure you extensively test if you do go down that path or try SharpZipLib instead.

For those targeting .Net Standard 2.0, SharpZipLib does a great work, handling gracefully extraction of in memory password protected zip files to byte[].
https://github.com/icsharpcode/SharpZipLib
Tried Ionic for the same scenario but was enable to extract files with the ZipInputStream, which generated corrupt extracted byte arrays.

I found the way is very simple to unzip in c#
Install PM (you can find new version if available!)
Install-Package Ionic.Zip -Version 1.9.1.8
Code C#
string zipFile = #"C:\Users\Fesslersoft\Desktop\ZipTest\Test.zip";
string targetDirectory = #"C:\Users\Fesslersoft\Desktop\ZipTest\";
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(zipFile))
{
zip.Password = "1234";
zip.ExtractAll(targetDirectory, Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
}
Console.WriteLine("Zip file has been successfully extracted.");
Console.Read();
Source: https://codesnippets.fesslersoft.de/extract-a-password-protected-zip-file-using-dotnetzip/

Related

Package whole C# Console application in one file?

Is it possible to package a whole c# console application with it's applications folder/files into one executable .exe file so it dosen't need to be installed on the computer when it's going to run?
If it doesn't have a lot of resource DLLs (with translated strings, in separate subfolders) then you can use ILMerge to do this for the DLL files:
http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx
ILMerge is a utility that can be used to merge multiple .NET
assemblies into a single assembly.
If you have other files too, you will have to add them as resources to your program and extract them at run-time.
See here for more details on how to embed resource files and extract them at run-time:
http://support.microsoft.com/kb/319292
Briefly: After you have added an embedded resource to your project, you do this:
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("MyNamespace.MyResourceFileName");
// ... do something with stream, e.g. write it out to a file.
[EDIT]
You can actually do away with ILMerge altogether and have a much more robust solution.
See here for details: http://blog.nenoloje.com/2011/08/better-alternative-to-ilmerge.html
And here: http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
The author of ILMerge says of this "As the author of ILMerge, I think this is fantastic! If I had known about this, I never would have written ILMerge"
It's written by Jeffrey Richter, who I hope most people here will have heard of. :)

How do I parse information from a zip or compound file in c#?

I'm new to c# and programming in general. I've started to create my own powerpoint plugin. I've got it to where a user can browse for a zip file but now I need to know - How do I parse information from a file within that zip or compound file in c#?
Do I have to use a library? If so how do I go about that?
As others have mentioned, the System.IO.Compression namespace has the ZipFile class for creating/extracting zip files. However, they are only available in .NET 4.5 however, which is currently only available as part of the Visual Studio 2012 RC.
See System.IO.Compression in:
.NET 4
.NET 4.5
Up until this point, the de-facto standard for working with zip files in .NET has been the DotNetZip Library. It is available under the open-source MS-PL license.
Also see:
Handling Zip Files Without Third Party Lib in .NET 4.0?
To work with ZIP-files in general, use the ZipArchive class that represents a ZIP file, or the members of the static ZipFile class to work with ZIP files, and use the DeflateStream class to read the contents of a compressed file.
To easily read the contents of a binary file, use the BinaryReader class. Otherwise, use the StreamReader class for text files.

extract APK file contents using .NET tool

I used apktool to extract an APK file contents. But for some reasons I need such a tool written in .NET so I do not need to install the JDK in the server.
Is there such a tool?
No. There is no such a tool. You need to start a Process in C# and run the batch file with the arguments using ProcessStartInfo class
I think in another approach, take the APK file and create a Java program that extracts the info I need such as package name, icon resource etc... and then expose it as COM object and use it in my .NET project.
After deep research we can not extract that info in standard functions in Java, since the only way to extract the info is to unzip the APK via the APKTool and read the manifest from the file system.
Another way but hard is to write your own extractor which involves in deep knowledge in the format of the APK.

What c# libraries can I use to create .bundle (zipped) files for use in iPhone SDK

I've just created a "bundle creator" app and I'd like to be able to produce a zipped .bundle file rather than have to copy the whole bundle folder.
ICSharpCode.SharpZLib Zip library to scuessfully create a archive file however the iPhone doesnt appear to uncompress it.
ZipArchive on Google Code might help you. Here is sample code.
SharpZipLib works well.
Edit: OK, so it didn't work for you I see in your edit ... but it's worked well for me in other cases (by which I mean non-iPhone).

What is the recommended way to package up a C# Class Library (DLL)?

I am creating a C# class library for use by other developers. How should I package it up - just zip up the DLL and put it on the website?
The recommended way is to upload the code source including build scripts to a web site such as googlecode so that other developers could download it and compile it but if it is closed source library zipping the assembly (mylibrary.DLL), documentation (mylibrary.XML) and debug information file (mylibrary.PDB) should be enough.
If it is just a DLL then I wouldn't bother zipping it. However it would be nice if you did zip up some documentation with it along with a sample configuration file if applicable.
Well, have you ever used another third party one? What would you like to see in the perfectly packaged third party download?
I'd zip it up with the following in at a minimum:
your dll in a /bin/ folder
readme (txt or html) explaining what it is, how to install and where to get more info
if you are including the source then
* source in /source/ folder
* Any tests in a /tests/ folder
If its something good you might want to stick it up on google code or codeplex rather than your own site?
Yes.
You should include documentation in your ZIP file, including a link back to your website. NDoc is great for generating documentation from your XML comments.
Look at 3rd party prijects for inspiration, but yes, a zipped dll is fine.
If your documentation is large then you should put it into a separate zip, and if your releasing the source you should put that into a 3rd zip.
Either way your dll should comes with a readme describing what version it is, what the purpose is, who wrote it and how to get in touch with them, along with any dependencies or other useful snippets of information.
Documentation is really important!
I would suggest that, if this is a product, you would want a setup project that installs it. This would include placing it in the GAC and leaving documentation somewhere.
edit
Just to clarify, I mean real documentation, not just what's automatically generated from comments.

Categories

Resources