Compressing and Decompressing Folders in C# - 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.

Related

C# re-zipping Docx after image replace won't open [duplicate]

I have been trying to write a simple Markdown -> docx parser/writer, but am completely stuck with the last part, which should be the easiest: i.e. compressing the folder into a .docx that Word, or any other .docx reader, will recognize.
My parser-writer is irrelevant really: I have this problem if I simply unzip any old Word-produced *.docx and then try to recompress it with the usual compression utilities, giving it the file-ending docx. Is there some mysterious header I should be adding, or do I need a special OPC compression utility, or what?
I don't so much want a tool that will do this, as to figure out what is supposed to be there. It seems to be independent of the WordprocessingML specification.
Needless to say I don't know anything about compression. Everything I can find via Google has to do with fancy utilities you can use in business, but I'm making a little executable that would be GPLd or something, and should work on anything.
The most common problem around manually zipping together Open XML documents is that it will not work if you zip the directory instead of the contents. In other words, the[content_types].xml file, and the word, docProps, and _rels directories need to reside at the root level of the zip file.
Here are steps to unzip my.docx and re-zip:
% mkdir unzipped
% cd unzipped/
% unzip ../my.docx
% zip -r ../rezipped.docx *
% open ../rezipped.docx
The compression algorithm used is "Zip" (Base 64) compression.
7zip seems to offer this, though i have no tested it.
Further to what Mica said, the contents of the ZIP file are organised according to the Open Packaging Convention; cf. Microsoft's Essentials of the Open Packaging Convention.
You can use the .NET System.IO.Packaging to make and manipulate .docx files; this class is implemented in the Mono project.

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).

How do you convert a folder to zip file?

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

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

"Carriage Return" gets stripped out when compresssing file

When compressing a string "stream" the '/r' gets stripped out from '/r/n'. I am using the ICSharp.zip library for compression. Has any one else faced this problem, and if you have is there is a workaround?
Does your zip library have a parameter to treat the stream as either text or binary? It sounds like it's treating it as text and is changing the line-end delimiter (some apps do this to try and make sure it matches the target platform). If you can tell it to treat the data as binary it might help.
Try DotNetZip.
It's a managed-code library, doesn't have problems with CR/LF translation.
fee. open source.
DotNetZip on CodePlex

Categories

Resources