英文:
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());
}
}
-
Is Deflatorstream in C# right analog? How I understand deflate and inflate are methods for compression and decompression.
-
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. -
What analog of java ByteArrayOutputStream in C#?
答案1
得分: 0
- 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();
}
}
- 我不了解您提供的库。您可以在nuget.org上搜索dotnet库。这是Visual Studio中NuGet的默认来源。您可以在那里找到SharpZipLib,它具有Inflater/Deflater和流功能。
- 您应该使用
System.IO.MemoryStream
。根据我刚刚搜索的内容,它看起来将ByteArrayOutputStream
和ByteArrayInputStream
的功能结合在一起。
英文:
- 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();
}
}
- 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.
- You should use
System.IO.MemoryStream
. From what I just googled it looks like it combines the functionality ofByteArrayOutputStream
andByteArrayInputStream
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论