java.util.zip.deflater equivalent in c# - c#

does anyone know how can I achieve java's Deflater.deflate() functionality in .NET so it would be understandable for java's Infalter.inflate() method?
regards,
Rafal

I have used #zipLib. It is pretty straight forward.
Taken from their site:
#ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language). The creator of #ziplib put it this way: "I've ported the zip library over to C# because I needed gzip/zip compression and I didn't want to use libzip.dll or something like this. I want all in pure C#."

ZlibStream class inside DotNetZip package (https://dotnetzip.codeplex.com/) is equivalent to java.util.zip.deflater.
Nuget: Install-Package DotNetZip
usage for byte array:
ZlibStream.CompressBuffer(dataBytesArray);
ZlibStream.UncompressBuffer(dataBytesArray);
it also has String compression and decompression, and the class can be used with streams exactly the same way as .NET DefalteStream. Please note that DeflateStream of DotNetZip is not the same as its Java, ZlibStream is.
Additional Info:
DeflateStream of .NET is not compatible with Deflate in Java. In fact, Java uses Zlib and adds 2-6 bytes header and 4 bytes checksum by default. Cutting off the bytes (suggested by some articles like http://blogs.msdn.com/b/bclteam/archive/2007/05/16/system-io-compression-capabilities-kim-hamilton.aspx) will work, but I don't suggest it as parsing header length may cause bugs.
I don't suggest SharpZipLib as it is pure C# and usually performance is important working with compression and decompression data. Look at http://www.codeproject.com/Articles/434583/SharpZipLib-or-DotNetZip-Which-should-you-use

Check System.IO.Compression namespace. It has DeflateStream. DeflateStream uses Deflate algorithm for compression, and so does java.util.zip.deflater. So you can compress with .NET and decompress with Java implementation and vice versa.

There's
ZipPackage Class
or
GZipStream Class
which might help, but I don't know how easy they are to use or if compatible with java (sorry not a great answer).
There's a nice blog post about zip stuff in: http://blogs.msdn.com/b/bclteam/archive/2010/06/28/working-with-zip-files-in-net.aspx
Or a few open source zip utilities around for c#.

Related

LZRW1 decompression in .NET

I have a database with data packed with LZRW1 algorithm and I need to read this data in a C# application. Does anyone know if there is a ready implementation of the LZRW1 decompression for C#?
I had a good look, I can't find any in C# or Java. I suspect it's a generational issue - that specific algorithm has been surpassed by ZIP etc and there was little reason to write the code in newer languages.
Below are some implementations in C++ and Pascal:
http://www.ross.net/compression/lzrw1.html
http://www.programmersheaven.com/download/14494/download.aspx

What is an alternate of ionic zip in C#?

I use Ionic.Zip to zip and unzip my data. But I find that Ionic.Zip is not capable of handling large file sizes (> 3GB).
So is there any third party tool that we can use to replace Ionic.Zip?
If you can use .NET Framework 4.5+, ZipArchive is now part of the BCL, as described by the "What's New in the .NET 4.5 Base Class Library" article.
Also, IMHO the 'de facto' third-party library for this is SharpZipLib from the SharpDevelop team.
It can actually handle larger files.
For that you need to use property:
UseZip64WhenSaving = Zip64Option.Always
Xceed realtimes zip for .NET
Quote from their website:
The size or number of files in a Zip archive has absolutely no impact on memory and disk-space requirements.
Might be cheaper options out there.
System.IO.Compression Namespace has DeflateStream and GZipStream classes.

Compression Assemblies for C#/.Net?

I know about SharpZipLib...but what else is out there? I'd like to make myself a file archiving utility that supports multiple compression formats. Any ideas?
QuickLZ claims to be the fastest in the world.
http://www.quicklz.com/
How about System.IO.Packaging?
http://msdn.microsoft.com/en-us/library/system.io.packaging.aspx
There's the built-in System.IO.Compression library which supports GZip and Deflate. That's not a bad place to start as its documentation is better than most.
LZMA SDK (7-Zip format) - one of the more efficient algorithms out there.
http://www.7-zip.org/sdk.html
http://dotnetzip.codeplex.com/ <-- imho easier than sharpziplib
http://sevenzipsharp.codeplex.com/ <-- wrapper around 7zip
http://www.chilkatsoft.com/zip-dotnet.asp <-- not free. bz2, gzip and .z

Good zlib implementation in .NET?

I'm building an network application that needs to be able to switch from normal network traffic to a zlib compressed stream, mid stream. My thoughts on the matter involve boolean switch that when on will cause the network code to pass all the data through a class that I can feed IEnumerable<byte> into, and then pull out the decompressed stream, passing that on to the already existing protocol parsing code.
Things I've looked at:
ZLib.NET - It seems a little... Ecclectic, and not quite what I want. Would still make a decent start to build off though. (Jon Skeet's comments here hardly inspire me either.)
SharpZipLib - This doesn't seem to support zlib at all? Can anyone confirm or deny this?
I would very much prefer and all managed solution, but let's have at it... are there any other implementations of this library out there in .NET, that might be better suited to what I want to do, or should I take ZLib.NET and build off that as a start?
PS:
Jon's asked for more detail, so here it is.
I'm trying to implement MCCP 2. This involves a signal being sent in the network stream, and everything after this signal is a zlib compressed data stream. There's links to exactly what they mean by that in the above link. Anyway, to be clear, I'm on the recieving end of this (client, not server), and I have a bunch of data read out of the network stream already, and the toggle will be in the middle of this (in all likelyhood atleast), so any solution needs to be able to have some extra data fed into it, before it takes over the NetworkStream (or I manually feed in the rest of the data).
SharpZipLib does support ZLib. Look in the FAQ.
Additionally, have you checked whether the System.IO.Compression namespace supports what you need?
I wouldn't use an IEnumerable<byte> though - streams are designed to be chained together.
EDIT: Okay... it sounds like you need a stream which supports buffering, but with more control than BufferedStream provides. You'd need to "rewind" the stream if you saw the decompression toggle, and then create a GZipStream on top of it. Your buffer would need to be at least as big as your biggest call to Read() so that you could always have enough buffer to rewind.
Included in DotNetZip there is a ZlibStream, for compressing or decompressing zlib streams of data. You didn't ask, but there is also a GZipStream and a DeflateStream. As well as a ZlibCodec class, if that is your thing. (just inflates or deflates buffers, as opposed to streams).
DotNetZip is a fully-managed library with a liberal license. You don't need to use any of the .zip capability to get at the Zlib stuff. And the zlib stuff is packaged as a separate (smaller) DLL just for this purpose.
I can recommend you Gerry Shaw's zlib wrapper for .NET:
http://www.organicbit.com/zip/
As far as I know the ZLib (gzip) library doesn't support listing the files in the header. Assuming that matters to you, but it seems a big shortcoming. This was when I used the sharp zip library a while ago, so I'm willing to delete this :)
Old question, but System.IO.Compression.DeflateStream is actually the right answer if you need proper zlib support:
Starting with the .NET Framework 4.5, the DeflateStream class uses the
zlib library. As a result, it provides a better compression algorithm
and, in most cases, a smaller compressed file than it provides in
earlier versions of the .NET Framework.
Doesn't get better than that.

Are there any compression and encryption libraries in C#?

I want to compress some files (into the ZIP format) and encrypt them if possible using C#. Is there some way to do this?
Can encryption be done as a part of the compression itself?
For compression, look at the System.IO.Compression namespace and for encryption look at System.Security.Cryptography.
For Zip Compression, have you seen http://www.icsharpcode.net/OpenSource/SharpZipLib/
I know the question is already old, but I must add my two cents.
First, some definitions:
Zip: Archive format for regrouping files and folders into a single file, and optionally encrypting data.
Deflate: One of the compression algorithms used within a Zip file to compress the data. The most popular one.
GZip: A single file compressed with deflate, with a small header and footer.
Now, System.IO.Compression does not do Zip archiving. It does deflate and gzip compression, thus will compress a single blob of data into another single blob of data.
So, if you're looking for an archive format that can group many files and folders, you need Zip libraries like:
Xceed Zip (it does support strong encryption)
SharpZipLib
If you only need to compress and encrypt a single blob of data, then look under System.IO.Compression and System.Security.Cryptography.
The GZipStream class is a native way to handle compression.
As for encryption, there are many ways to do it, most of them in the System.Security namespace. They can be done chained (encrypt a compressed stream or compress an encrypted stream).
Chilkat provides .NET libraries for compression and encryption.
I'm not sure if the steps can be combined, but .NET has good support for basic crypto. Here's an article on it.
If they cannot be combined, do compression first and then encryption. Compressing an already encrypted file will lead to poor compression ratios, because a lot of redundancy is removed.
Here is a useful topic:
Help in creating Zip files from .Net and reading them from Java
System.IO.Packaging namespace gives you useful classes to compress data in zip format and support rights management.
There isn't anything you can use directly in C#, however you can use some libraries from J# to do it for you:
http://msdn.microsoft.com/en-us/magazine/cc164129.aspx
Should do just what you want?
With regards to the encryption, have a look at these links:
http://www.codeproject.com/KB/security/fileencryptdecrypt.aspx
http://www.obviex.com/samples/EncryptionWithSalt.aspx

Categories