T
Tony Johansson
Hello!
In this program I compress the string "Test" by using DeflateStream in
method Compress.
I decompress in three different ways by using three different methods.
All three methods works and give back the original string.
My question is concering the last method Decompress3 if it's possible to
reduce the code here because I use the returned
value from DeflateStream.Read to know how many bytes the actual string is
and then I use a for loop to extract these ?
I assume that Decompress3 can't be done in a better way.
public static byte[] Compress(string value)
{
byte[] document = new UTF8Encoding().GetBytes(value);
MemoryStream strm = new MemoryStream();
DeflateStream deflate = new DeflateStream(strm,
CompressionMode.Compress);
deflate.Write(document, 0, document.Length);
deflate.Close();
byte[] result = strm.ToArray();
//string s = System.Text.ASCIIEncoding.UTF8.GetString(result);
return result;
}
public static string Decompress1(byte[] value)
{
MemoryStream ms = new MemoryStream(value);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
StreamReader reader = new StreamReader(ds);
string result = reader.ReadToEnd();
ds.Close();
ms.Close();
return result;
}
public static string Decompress2(byte[] value)
{
MemoryStream ms = new MemoryStream(value);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
int myByte = ds.ReadByte();
string result = string.Empty;
while (myByte != -1)
{
result += (char)myByte;
myByte = ds.ReadByte();
}
ds.Close();
ms.Close();
return result;
}
public static string Decompress3(byte[] value)
{
MemoryStream ms = new MemoryStream(value);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
byte[] data = new byte[ms.Length];
int rByte = ds.Read(data, 0, data.Length);
StringBuilder sb = new StringBuilder(rByte);
for (int i = 0; i < rByte; i++)
sb.Append((char)data);
string result = sb.ToString();
ds.Close();
ms.Close();
return result;
}
static void Main(string[] args)
{
string input = "test";
byte[] compressedData = Compress(input);
if (input == Decompress1(compressedData))
Console.WriteLine("Equal");
if (input == Decompress2(compressedData))
Console.WriteLine("Equal");
if (input == Decompress3(compressedData))
Console.WriteLine("Equal");
}
//Tony
In this program I compress the string "Test" by using DeflateStream in
method Compress.
I decompress in three different ways by using three different methods.
All three methods works and give back the original string.
My question is concering the last method Decompress3 if it's possible to
reduce the code here because I use the returned
value from DeflateStream.Read to know how many bytes the actual string is
and then I use a for loop to extract these ?
I assume that Decompress3 can't be done in a better way.
public static byte[] Compress(string value)
{
byte[] document = new UTF8Encoding().GetBytes(value);
MemoryStream strm = new MemoryStream();
DeflateStream deflate = new DeflateStream(strm,
CompressionMode.Compress);
deflate.Write(document, 0, document.Length);
deflate.Close();
byte[] result = strm.ToArray();
//string s = System.Text.ASCIIEncoding.UTF8.GetString(result);
return result;
}
public static string Decompress1(byte[] value)
{
MemoryStream ms = new MemoryStream(value);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
StreamReader reader = new StreamReader(ds);
string result = reader.ReadToEnd();
ds.Close();
ms.Close();
return result;
}
public static string Decompress2(byte[] value)
{
MemoryStream ms = new MemoryStream(value);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
int myByte = ds.ReadByte();
string result = string.Empty;
while (myByte != -1)
{
result += (char)myByte;
myByte = ds.ReadByte();
}
ds.Close();
ms.Close();
return result;
}
public static string Decompress3(byte[] value)
{
MemoryStream ms = new MemoryStream(value);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
byte[] data = new byte[ms.Length];
int rByte = ds.Read(data, 0, data.Length);
StringBuilder sb = new StringBuilder(rByte);
for (int i = 0; i < rByte; i++)
sb.Append((char)data);
string result = sb.ToString();
ds.Close();
ms.Close();
return result;
}
static void Main(string[] args)
{
string input = "test";
byte[] compressedData = Compress(input);
if (input == Decompress1(compressedData))
Console.WriteLine("Equal");
if (input == Decompress2(compressedData))
Console.WriteLine("Equal");
if (input == Decompress3(compressedData))
Console.WriteLine("Equal");
}
//Tony