从Java到C#。在C#中模拟java.util.zip.Inflater,就像Java中的那样。

huangapple go评论123阅读模式
英文:

From Java to C#.Analog of java.util.zip.Inflater from java in C#

问题

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

...

public static byte[] DecompressByZLIB(byte[] compressedBytes)
{
    try
    {
        using (MemoryStream outputStream = new MemoryStream())
        {
            using (MemoryStream compressedStream = new MemoryStream(compressedBytes))
            using (DeflateStream inflater = new DeflateStream(compressedStream, CompressionMode.Decompress))
            {
                byte[] buffer = new byte[1024];

                int count;
                while ((count = inflater.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outputStream.Write(buffer, 0, count);
                }
            }

            return outputStream.ToArray();
        }
    }
    catch (Exception exception)
    {
        throw new InvalidOperationException(exception.Message);
    }
}
// 1. In C#, you can use the DeflateStream class to achieve the same functionality as Inflater and Deflator in Java.

// 2. The zlib compression algorithm is available in C# through the DeflateStream class. You don't need an external library.

// 3. The equivalent of Java's ByteArrayOutputStream in C# is MemoryStream.

(Note: The provided code assumes you have included the necessary namespaces for using MemoryStream and DeflateStream.)

英文:

I'm trying to translate Java code to C# and find problem with Infater. It's my task with binary work (sorry if it silly question)

Original code:

public static byte[] decompressByZLIB(byte[] compressedBytes)
{
	try  
    {
		Inflater inflater = new Inflater();
		inflater.setInput(compressedBytes);

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(compressedBytes.length);

		byte[] buffer = new byte[1024];

		while(!inflater.finished()) 
        {
			int count = inflater.inflate(buffer);
			byteArrayOutputStream.write(buffer, 0, count);
		}

		byteArrayOutputStream.close();
		return byteArrayOutputStream.toByteArray();
	} 
    catch (Exception exception)
    {
		throw new IllegalStateException(exception.getMessage());
	}
}
  1. Is Deflatorstream in C# right analog? How I understand deflate and inflate are methods for compression and decompression.

  2. In original code mentioned Zlib. How I understand It is what I need.
    I can't use Lib so I tried to understand this file http://www.componentace.com/.NET_zip_component_zipforge.htm, but I think I don't have enough experience to understand how all methods are working.

  3. What analog of java ByteArrayOutputStream in C#?

答案1

得分: 0

  1. DeflateStream类使用zlib库,因此在这应该能工作(我对Java不熟悉)。DeflateStream是另一个包含压缩数据或可以接收数据的流的包装器。使用DeflateStream时,从流中读取时进行解压缩,向流中写入时进行压缩。
    以下是.NET Core的示例:
        public static byte[] Decompress(byte[] data)
        {
            using (var compressedDataStream = new MemoryStream(data))
            using (var deflateStream = new DeflateStream(compressedDataStream, CompressionMode.Decompress))
            using (var outputStream = new MemoryStream())
            {
                // 从基础流(compressedDataStream)中读取发生在CopyTo中,
                // 解压缩数据被复制到outputStream中
                deflateStream.CopyTo(outputStream);

                return outputStream.ToArray();
            }
        }
    
  1. 我不了解您提供的库。您可以在nuget.org上搜索dotnet库。这是Visual Studio中NuGet的默认来源。您可以在那里找到SharpZipLib,它具有Inflater/Deflater和流功能。
  2. 您应该使用System.IO.MemoryStream。根据我刚刚搜索的内容,它看起来将ByteArrayOutputStreamByteArrayInputStream的功能结合在一起。
英文:
  1. DeflateStream class uses the zlib library so it should work here (I'm not familiar with Java). DeflateStream is a wrapper for another stream that contains compressed data or can receive data. When using DeflateStream you decompress when reading from the stream and compress when writing to the stream.
    Here is an example in .NET Core:
        public static byte[] Decompress(byte[] data)
        {
            using (var compressedDataStream = new MemoryStream(data))
            using (var deflateStream = new DeflateStream(compressedDataStream, CompressionMode.Decompress))
            using (var outputStream = new MemoryStream())
            {
                // reading from base stream (compressedDataStream) happens in CopyTo 
                // and decompressed data is copied to outputStream
                deflateStream.CopyTo(outputStream);

                return outputStream.ToArray();
            }
        }
    
  1. I don't know the library that you linked. You can search for dotnet libraries on nuget.org. It is default source of NuGets in Visual Studio. You can find SharpZipLib there which has Inflater/Deflater and streams.
  2. You should use System.IO.MemoryStream. From what I just googled it looks like it combines the functionality of ByteArrayOutputStream and ByteArrayInputStream.

huangapple
  • 本文由 发表于 2020年10月20日 23:15:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定