Trying To Gzip and Unzip but getting different content - c#

I have the following functions to gzip and unzip text content, however the uncompressed output is not complete, missing approx the last 10th of the file (long html file). What am I doing wrong?
private byte[] GZipString(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
using (var mso = new MemoryStream())
using (var gz = new GZipStream(mso, CompressionMode.Compress))
{
gz.Write(bytes, 0, bytes.Length);
return mso.ToArray();
}
}
private string UnzipFile(string filename)
{
var bytes = File.ReadAllBytes(filename);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
using (var gz = new GZipStream(msi, CompressionMode.Decompress))
{
gz.CopyTo(mso);
return Encoding.UTF8.GetString(mso.ToArray());
}
}

Related

How to convert from a gzip compressed file to Zstandard

How do I convert from a gzip compressed file to Zstandard? Here is my attempt:
using Zstandard.Net;
public static void ConvertToZstd(string path)
{
byte[] compressed = null;
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
using (var memoryStream = new MemoryStream())
using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
{
compressionStream.CompressionLevel = 11; // optional!!
//compressionStream.CompressionDictionary = dictionary; // optional!!
compressionStream.Write(gzipStream, 0, gzipStream.Length);
compressionStream.Close();
compressed = memoryStream.ToArray();
}
}
public static void ConvertToZstd(string path, string outputPath)
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
using (var memoryStream = new MemoryStream())
using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
{
compressionStream.CompressionLevel = 11; // optional!!
//compressionStream.CompressionDictionary = dictionary; // optional!!
gzipStream.CopyTo(compressionStream);
compressionStream.Close();
File.WriteAllBytes(outputPath, memoryStream.ToArray());
}
}

How do I use System.Io.Compression to replace GzipDotNet.dll

I am working on an existing system where data is stored in a compressed byte array in a database.
The existing data has all been compressed using GZipDotNet.dll.
I am trying to switch to using the gzip functions in System.IO.Compression.
When I use:
public static byte[] DeCompressByteArray(byte[] inArray)
{
byte[] outStream = null;
outStream = GZipDotNet.GZip.Uncompress(inArray);
return outStream;
}
It works fine but:
public static byte[] DeCompressByteArray(byte[] inArray)
{
byte[] outStream = null;
using (var compressedStream = new MemoryStream(inArray))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
outStream = resultStream.ToArray();
}
return outStream;
}
Gives a response of:
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream

C# can't seem to correctly add images to ziparchive

I'm trying to add a bunch of files to a zipfile in c# but it doesn't seem to work properly.
using (var memoryStream = new MemoryStream())
{
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < kaart_data.GetLength(0); i++)
{
Image img = array[i];
var file = zip.CreateEntry(i + ".bmp");
using (var stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Bmp);
using (var entryStream = file.Open())
{
stream.CopyTo(entryStream);
}
}
}
}
//saves the archive to disk
using (var fileStream = new FileStream(#"C:\Temp\test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
The thing is it does create and save the zip file into disk with the expected file size.
But when I try to open them in windows photo viewer they seem to be corrupted.
Any help is appreciated.
It almost took 3 hrs to figure out what's the issue. If u look at the the size of original image and extracted one, there's a tiny difference.
using (var memoryStream = new MemoryStream())
{
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (var i = 0; i < images.Length; i++)
{
var img = images[i];
var file = zip.CreateEntry(i + ".bmp");
using (var stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Bmp);
using (var entryStream = file.Open())
{// to keep it as image better to have it as bytes
var bytes = stream.ToArray();
entryStream.Write(bytes, 0, bytes.Length);
}
}
}
}
using (var fileStream = new FileStream(#"test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
I've tried it it works like a charm!

Gzipped content doesn't reflect input

I have a very simple gzip method:
public byte[] Compress(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
using (var gz = new GZipStream(mso, CompressionMode.Compress))
{
msi.CopyTo(gz);
return mso.ToArray();
}
}
However, unit tests fail. Even passing in a simple short string doesn't get gzipp'ed properly. e.g. "this is a test" becomes a byte array with 10 elements: [31,139,8,0,0,0,0,0,4,0] which of course doesn't ungzip properly. What's going wrong here? This has come straight from msdn!
You need to flush close the stream for it to compress. At the point you call mso.ToArray(), the GZipStream hasn't compressed anything yet and is waiting for more data.
A simple solution:
public byte[] Compress(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gz = new GZipStream(mso, CompressionMode.Compress))
{
msi.CopyTo(gz);
}
return mso.ToArray();
}
}

Decompress byte array to string via BinaryReader yields empty string

I am trying to decompress a byte array and get it into a string using a binary reader. When the following code executes, the inStream position changes from 0 to the length of the array, but str is always an empty string.
BinaryReader br = null;
string str = String.Empty;
using (MemoryStream inStream = new MemoryStream(pByteArray))
{
GZipStream zipStream = new GZipStream(inStream, CompressionMode.Decompress);
BinaryReader br = new BinaryReader(zipStream);
str = br.ReadString();
inStream.Close();
br.Close();
}
You haven't shown how is the data being compressed, but here's a full example of compressing and decompressing a buffer:
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
class Program
{
static void Main()
{
var test = "foo bar baz";
var compressed = Compress(Encoding.UTF8.GetBytes(test));
var decompressed = Decompress(compressed);
Console.WriteLine(Encoding.UTF8.GetString(decompressed));
}
static byte[] Compress(byte[] data)
{
using (var compressedStream = new MemoryStream())
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
{
zipStream.Write(data, 0, data.Length);
zipStream.Close();
return compressedStream.ToArray();
}
}
static byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
}

Categories

Resources