Serialization using GZipStream [duplicate] - c#

I've got some strange problem with GZip Serializer.
Trying serializing object with data in it.
Following code give results(at POINT1 in debug): ms.Length = 100028 and uncompressedStream.Length=100027
After POINT1 there is exception "End of Stream encountered before parsing was completed.", which i think is result of this lost byte.
I am using .net 4.0.
//generating data
int length = 100000;
byte[] data = new byte[length];
for (int i = 0; i < length; i++)
{
data[i] = System.Convert.ToByte(i % 100 + i % 50);
}
//serialization into memory stream
IFormatter formatter = new BinaryFormatter();
var ms = new MemoryStream();
formatter.Serialize(ms, data);
ms.Seek(0, SeekOrigin.Begin);
//GZip zip
MemoryStream compressedStream = new MemoryStream();
var Compress = new GZipStream(compressedStream, CompressionMode.Compress);
ms.CopyTo(Compress);
compressedStream.Seek(0, SeekOrigin.Begin);
//GZip Unzip
MemoryStream uncompressedStream = new MemoryStream();
var Decompress = new GZipStream(compressedStream, CompressionMode.Decompress);
Decompress.CopyTo(uncompressedStream);
uncompressedStream.Seek(0, SeekOrigin.Begin);
//deserialization from memory stream
//POINT1
var oo = formatter.Deserialize(uncompressedStream);
var o = (byte[])oo;
//checking
Assert.AreEqual(data.Length, o.Length);
for (int i = 0; i < data.Length; i++)
Assert.AreEqual(data[i], o[i]);

Compression streams don't flush (and can't properly flush) until they are closed. You will need to close the GZipStream. Telling it not to close the underlying stream (one of the constructor arguments) will make this easier.
//generating data
int length = 100000;
byte[] data = new byte[length];
for (int i = 0; i < length; i++)
{
data[i] = System.Convert.ToByte(i % 100 + i % 50);
}
byte[] o;
//serialization into memory stream
IFormatter formatter = new BinaryFormatter();
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, data);
ms.Seek(0, SeekOrigin.Begin);
//GZip zip
using(MemoryStream compressedStream = new MemoryStream())
{
using (var Compress = new GZipStream(compressedStream, CompressionMode.Compress, true))
{
ms.CopyTo(Compress);
}
compressedStream.Seek(0, SeekOrigin.Begin);
//GZip Unzip
using (MemoryStream uncompressedStream = new MemoryStream())
{
using (var Decompress = new GZipStream(compressedStream, CompressionMode.Decompress, true))
{
Decompress.CopyTo(uncompressedStream);
}
uncompressedStream.Seek(0, SeekOrigin.Begin);
var oo = formatter.Deserialize(uncompressedStream);
o = (byte[])oo;
}
}
//deserialization from memory stream
//POINT1
}
//checking
Debug.Assert(data.Length == o.Length);
for (int i = 0; i < data.Length; i++)
Debug.Assert(data[i] == o[i]);

Related

How to fix 'Found invalid data while decoding.'

When I am trying to deflate one string to XML getting the error. can anyone help me out, if the string is wrong or any other thing I need to consider?
string output = DecompressString("H4sIAAAAAAAAAO1ay3LbIBTd9ys83hOcZrrxKP6CZpUuusUStulIoAFkxfn6XpCQ0NvWTDt+ZBUJ7vtwEQcneHv7ReN3Ko8spGrxkcRcrT8Ue10etE7XGOd5/pS/PAm5x99Xq2f8++3ne3igCUGMK014SJeFVqWRSfZEtWJWh+85TokkicIghRVLFPiCl5Cky823xSIIRZLGVFMkJNszTjTje8QiyjXTJ5RKquARRgU34qAwKbcgoWZH+rrUMqNL7NRSKokWctrPgmT6AFKfNCptFCbACEhpyUIjhcSRSgkGNu4BcaFR4TvAvZLOTEjimEoUMZXG5IQ4SaiLYGMcBnhMosgHn5+QrTO+rNATuHjpnQuLr1NBtCOxmoNR05iHV2GwqnQioOxgNSEcVANs33vQ3EC9wQE7EsiY8Z0ADTPeAPLyyrfrNIVCW7yShock4ywsrEZQO6nqiAZmm41Qp53FEKF2bZtJvmZU79ZFn65heA2bgLEJxlIRs/BUKZfqCwalNmsUQaVyIiOTRcZDwSNmYiAx2p7QEfISnqqNtJRQ/rABikaMzN9JrCuA11hpWTbxoojaWgC60WwnuJEJ7k8lIBY95QnDGoIQZlWQZBH7TxW0rm6+gttMne596YFhk+YNoHDPy/f6UeACvidp/Aj9AKkSrnIqbwSOO2+MW4ADzjYZOLSHdgk1OJAtVOMRWsXL9xbxufveuR58ijfgCo75jNOQihcN8pFhotZUkcB5eMGdOdCxoyjGN2FMSdt8QwKJXe2vHe+Q1ZoPuvn8QMu4LT+T9A8NAWREdDkKz1rYBbqB2oJQRs0AAdKuPUUjYCjkHLsuKi00kAGeJVvoBj89ZdoFmaXSrMjmeRXgy7UqJEAUqhO5qmmWQDQvYLN/ph/BZidX375CZdVGZlTaD4zD2XK3g5RqQ93RNk0fWbUeIR+TGhDKCTM49rLwcm7qpmNAafAGymyTbmVBlUIKxpXZJNnOGTkwhexuCn9dhNZ7ca00Q3u8oJ7UYDl9mQGRg4ij8eoYiW5p8KhjZ3XI65ZIWafIgIAnpkC9Qv/4PqX0YrrV1r+KBd7UvZ9Mpr5RrSCgJCKfn17ZDtZKM4qOu+ETyyRe93xSuRG8OieY8Q53+0+m9+JKtoE6lq9t4GqW1UV4fW0DV4UX45pKTh7jZwp7pvHy/UL9EX5aeWjUO5/88a95m3JMUYBe+uEbmDo8XMZfvChrCpNQpcieOlblcfJ2MMOS04GMe/EJFQfuTXlIu/TNzYww2oR8eIIIVoRmIUsJ1/YiY2S6S0v9QBolbUxU43Y7Hom/PT8vix8XJdEXk5dK73SdaKY0rN5Pc6UUl5dMWvAuMANyPTd0neU54sILpLxoODugCflzApt0GeDGfzttvv0F8ylWAP4kAAA=");
public static string DecompressString(string compressedString)
{
byte[] decompressedBytes;
var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString));
using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
using (var decompressedStream = new MemoryStream())
{
decompressorStream.CopyTo(decompressedStream);
decompressedBytes = decompressedStream.ToArray();
}
}
return Encoding.UTF8.GetString(decompressedBytes);
}
public static string DecodeGzip(string str)
{
byte[] gzBuffer = Convert.FromBase64String(str);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 0, gzBuffer.Length);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
int length;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
length = zip.Read(buffer, 0, buffer.Length);
}
var data = new byte[length];
Array.Copy(buffer, data, length);
return Encoding.UTF8.GetString(data);
}
}

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!

out of memory exception in compress large files using io.compression

i am trying to not exceeded memory max size so i have to check every time if It greater than Max Memory Size Then i flush it into zip file Stream . The Problem Here it replace memory stream with existence one in file stream ,Or Is there Any way To Do the Same Rquired with Another Way ( But With Out Using Any DLL Lib)
MemoryStream memoryStream = new MemoryStream();
FileStream fileStream = new FileStream(sbZipFolderName.ToString(),FileMode.Create);
foreach (FileInfo flInfo in ListfileFolderPaths)
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Update, true))
archive.CreateEntryFromFile(flInfo.FullName, slastFolderName + "/" + flInfo.DirectoryName.Replace(new DirectoryInfo(sFolderPath.ToString()).FullName, "") + "/" + flInfo.Name);
if (memoryStream.Length > MaxSize)
{
using (fileStream = new FileStream(sFolderPath + "/" + slastFolderName + ".zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
memoryStream = new MemoryStream();
}
}
}
if ((memoryStream != null) && (memoryStream.Length > 0))
memoryStream.CopyTo(fileStream);
You can use theGzip archive to compress a file.
This is the compression:
public static byte[] Compress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory,
CompressionMode.Compress, true))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
}
And this to decompression :
static byte[] Decompress(byte[] gzip)
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
}
Tell me if it worked.
Goodluck.

GZipStream decompress only 4kb of data

Why GZipStream, based on MemoryStream reads from it only 24576bytes and cant read any further? I compress my gz with multithreading, in parts, but winrar could decompress it and GZipStream not.
static private bool ZipperWork2(string InputPath, string OutputPath, CompressionMode CompMode)
{
Mode = CompMode;
RawData = new byte[ThreadCount][];
CompressedData = new byte[ThreadCount][];
Thread[] CompressorThreads = new Thread[ThreadCount];
byte[] buffer = new byte[MAX_BLOCK_SIZE];
using (FileStream fs = new FileStream(InputPath, FileMode.Open))
{
using (FileStream fw = new FileStream(OutputPath, FileMode.Append))
{
while (fs.Position < fs.Length)
{
for (int i = 0; i < ThreadCount; i++)
{
int count = fs.Read(buffer, 0, MAX_BLOCK_SIZE);
RawData[i] = new byte[count];
Array.Copy(buffer, RawData[i], count);
CompressorThreads[i] = new Thread(WorkWithBlock2);
CompressorThreads[i].Start(i);
}
for (int i = 0; i < ThreadCount; i++)
{
CompressorThreads[i].Join();
fw.Write(CompressedData[i], 0, CompressedData[i].Length);
}
}
}
}
return true;
}
private static void WorkWithBlock2(object index)
{
int DataIndex = (int)index;
//int count=0,totalcount=0;
using (MemoryStream ms = new MemoryStream(RawData[DataIndex]))
{
using (MemoryStream output = new MemoryStream())
{
using (GZipStream gz = new GZipStream(ms, Mode))
{
gz.CopyTo(output);
}
CompressedData[DataIndex] = output.ToArray();
}
}
}
I trying to do it in a different ways, but result are always the same, decompress only 4KB (and it don't relate with my MAX_BLOCK_SIZE variable)
Your gzip stream may actually consist of multiple concatenated gzip streams. This is permitted by the gzip format. GZipStream is likely just reading the first one. All you'd need to do is repeat the operation on the subsequent input bytes until all of the input bytes have been consumed.

GZipStream and decompression

I have code that should do the compression:
FileStream fs = new FileStream("g:\\gj.txt", FileMode.Open);
FileStream fd = new FileStream("g:\\gj.zip", FileMode.Create);
GZipStream csStream = new GZipStream(fd, CompressionMode.Compress);
byte[] compressedBuffer = new byte[500];
int offset = 0;
int nRead;
nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
while (nRead > 0)
{
csStream.Write(compressedBuffer, offset, nRead);
offset = offset + nRead;
nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
}
fd.Close();
fs.Close();
and I think it does, but I want to decompress what was compressed the way above. I do somethink like that:
FileStream fd = new FileStream("g:\\gj.new", FileMode.Create);
FileStream fs = new FileStream("g:\\gj.zip", FileMode.Open);
GZipStream csStream = new GZipStream(fs, CompressionMode.Decompress);
byte[] decompressedBuffer = new byte[500];
int offset = 0;
int nRead;
nRead=csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
while (nRead > 0)
{
fd.Write(decompressedBuffer, offset, nRead);
offset = offset + nRead;
nRead = csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
}
fd.Close();
fs.Close();
and here it doesn't... I've got nRead = 0 befeore entering the loop... What I do wrong??
The test file I use is the simpliest TEXT file (size: 104 bytes)...
My first thought is that you haven't closed csStream. If you use using this happens automatically. Since gzip buffers data, you could be missing some.
Secondly; don't increment offset; that is the offset in the buffer (not the stream). Leave at 0:
using (Stream fs = File.OpenRead("gj.txt"))
using (Stream fd = File.Create("gj.zip"))
using (Stream csStream = new GZipStream(fd, CompressionMode.Compress))
{
byte[] buffer = new byte[1024];
int nRead;
while ((nRead = fs.Read(buffer, 0, buffer.Length))> 0)
{
csStream.Write(buffer, 0, nRead);
}
}
using (Stream fd = File.Create("gj.new.txt"))
using (Stream fs = File.OpenRead("gj.zip"))
using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress))
{
byte[] buffer = new byte[1024];
int nRead;
while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0)
{
fd.Write(buffer, 0, nRead);
}
}
The two methods I have are like James Roland mentioned.
private static byte[] Compress(HttpPostedFileBase file)
{
using var to = new MemoryStream();
using var gZipStream = new GZipStream(to, CompressionMode.Compress);
file.InputStream.CopyTo(gZipStream);
gZipStream.Flush();
return to.ToArray();
}
private static byte[] Decompress(byte[] compressed)
{
using var from = new MemoryStream(compressed);
using var to = new MemoryStream();
using var gZipStream = new GZipStream(from, CompressionMode.Decompress);
gZipStream.CopyTo(to);
return to.ToArray();
}
However, I'm using an upload with
Request.Files[0]
then compress and save in the db. Then I pull the img out, decompress and set a src with
$"data:image/gif;base64,{ToBase64String(Decompress(img))}";

Categories

Resources