How do you convert a folder to zip file? - c#

What is the best way to convert a whole folder (including it's contents) into a .zip file?

There is no class for doing this in the .NET Framework itself, but you can use some of the third-party libraries. An open-source DotNetZip seems to be quite good and has a lot of examples to get you started. You'll just need to recursively iterate over all files in the given folder and add them one by one.

This appears to have your answer.

I've used the Xceed Zip libraries in the past for this and found them very easy to use. The full license is quite pricey, but they do have a 45 day, 100% functional free trial available here:
http://xceed.com/Zip_Net_Intro.html

Related

Alternatives to ZIP for combining many files into one on Windows using .NET

Im looking for methods to combine files including their name and relative path into one single file. A folder disguised as a file. I don't need any compression or encryption. Just the file data including some binary metadata attached to each file.
It would be great if this file was possible to open/inspect/unpack with a standard file browser in Windows such as with regular zip-files.
Yes I could use zip. But I'm researching alternatives and I would prefer a simple method I could implement myself in C#/.NET.
UPDATE
I've researched this some more and came across Microsoft's Structured Storage format. It looked promising at first but it seemes to be an obsolete format, replaced with the Open Package Format. And then I found out about the TAR-format. It seemes to be the most basic format. But I'm not sure yet if I can add any custom metadata to the entries with TAR.
UPDATE
I went with DotNetZip at the end anyway...
Why not use zip? You can use a third party library, like dotnetzip, to make the code easy to write. And, as you mentioned, Windows handles zip files well.
If you have specific reason to search an alternative to ZIP, take a look on virtual file systems, eg. CodeBase File System or our Solid File System. Solid File System lets you add alternate data streams (like in NTFS) or tags (small chunks of binary or text data) to each file or directory. And with OS edition of SolFS you can make the filesystem visible to Windows (including Explorer and third-party applications).
I must admit that while virtual file systems are easy to use (easier than ZIP), they are commercial products (I didn't see free virtual file system implementations yet).

Libraries to verify file format by header

A few times in the last couple months I had the same task of verifying the file format by it's header: JPEG, PDF, Word, and other popular files.
I wonder if there is a library availble for C#/.Net to do that?
Or is it a time to start a small project for NuGet catalogue?
For those who will find this question in the future: I have started to write the library. Once I have significant amount of different types, I'll submit it to NuGet. But at the moment the source code is available here: http://filetypedetective.codeplex.com/
The idea of the library is to be able to call isPdf() or isZip() on FileInfo objects:
FileInfo file = new FileInfo("C:\Hello.pdf");
if ( file.isPdf())
Console.WriteLine("File is PDF");
etc.
Update: the finally got around to create nuget package:
Install-Package FileTypeDetective
For most file formats, you can read the magic numbers at the beginning of the file to determine the file type. This is how *nix based systems know the file type regardless of the file extension.
The OP's solution is GPL'd, but a solution using the Win32 API and P/Invoke is available at https://gyorgybalassy.wordpress.com/2013/08/04/mime-types-sniffing-in-c-sharp/
It uses the FindMimeFromData function discussed at https://msdn.microsoft.com/en-us/library/ms775107(v=vs.85).aspx

Compressing and Decompressing Folders in C#

I want to compress and decompress a folder using C#. The problem with GZipStream is that it takes filenames and hence I need to write a recursive logic.
Can I somehow do it like, give source folder name and destination filename to compress the complete folder with hierarchy. I need to do vice-versa for de-compressing the folder as well.
If its not possible through C#/.net directly please suggest some Free 3rd party.
I've used the free SharpZipLib multiple times and I'd recommend that you take a look at it. It's quite easy to use and have worked well for all my use cases.
Now included in .NET 4.5 if you'd rather stay off non microsoft libs.
System.IO.Compression.ZipArchive Class
GZip only ever deals with single files, which is why under *nix you end up having to archive them into a TAR file first which is then compressed.
If you want multiple files/folders you'll need a format which supports it, like ZIP.
You might want to thus look at: http://www.sharpdevelop.net/OpenSource/SharpZipLib/
You could take a look at this library instead.
Take a look at DotNetZip Lib.

EPUB creation in .Net

Is there any library out there that I can use to create epub files in .NET/C#? A Flowdocument -> epub conversion tool would be ideal, but any kind of library would be great.
I am also interested in an assesment of how complex it would be to write one. I understand that it is basically a bunch of zipped XHTML files, but I keep hearing that using existing conversion tools it is difficult to get it right.
Did you try ePub Sharp? Here is the project page
Note: I have not tried it myself.
I am using the following library from Aspose - http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx
I build a DLL around epubcheck you can use from .NET by using IKVM.
You can download it from here:
http://www.raulvejar.com/Blog/tabid/70/EntryId/1/EPubCheck-1-1-on-NET.aspx
DotNetEpub may not have been around when the question was first asked.

c# file container

I am searching for a way to add several files into one file, much like a Zip file. I need to be able to create a file container on the fly and add several word documents, images and other important files into the container. My criteria is that you don't need to install any additional software on the computer (preferebly only a .DLL file that i can include in my project), that the program is free and that you can encrypt the data.
Anyone know of any good container programs that has support for these 2 criterias or if anyone know any good information about how to create your own container.
Patrick
Does it have to be like a zip file, or can it be a zip file?
Are you using .NET Framework 3.0 or 3.5? If so, look at
System.IO.Packaging.ZipPackage
This discussion has a section about it.
In addition to DotNetZip (licensed with Microsoft Public License) that Jay Riggs mentions, there's SharpZipLib (licensed with GPL). Whichever you choose, be sure the terms of the license match your understanding of the word "free".
If you can use ZipPackage, one benefit is that you don't need to think about license terms (beyond those of developing any other .NET app).
EDIT: DotNetZip and SharpZipLib support encryption. I don't see that ZipPackage does, but you could look at System.IO.Packaging.EncryptedPackageEnvelope.
I used DotNetZip in a project and it worked really well. I would recommend using it. It supports encryption and is easy to use.
http://www.codeplex.com/DotNetZip
User .Net GZipStream class(System.IO.Compression namespace.) to compress and decompress files. You can find more information on
MSDN Link
GZIP Compression
I have personally used this technique to decomress .zip file. Click Here

Categories

Resources