Why GZipStream remove whitespaces? - c#

I use GZipSream to compress and decompress text file. But there is no whitespaces in decompressed file.
My code for compress:
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.OpenOrCreate))
{
using (FileStream targetStream = File.Create(compressedFile))
{
using (GZipStream compressionStream = new GZipStream(targetStream, CompressionMode.Compress))
{
sourceStream.CopyTo(compressionStream);
}
}
}
For decompress:
using (FileStream sourceStream = new FileStream(compressedFile, FileMode.OpenOrCreate))
{
using (FileStream targetStream = File.Create(targetFile))
{
using (GZipStream decompressionStream = new GZipStream(sourceStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(targetStream);
}
}
}
Example uncompressed input:
2018-11-13 00:02:40,574 76 INFO
Decompressed:
2018-11-1300:02:40,57476INFO

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());
}
}

C# Cannot access a closed stream while copying GZipStream to MemoryStream

I've been trying to convert a GZipStream into a MemoryStream and then convert it to a byte array without having to write any files to the hard drive. I've been trying to copy it to a MemoryStream but I've been getting this error: Unhandled Exception: System.ObjectDisposedException: Cannot access a closed Stream.
I've looked at some of the other solutions, but I haven't been able to successfully implement them into what I'm trying to accomplish.
GZipStream decompressedStream = Decompress(new FileInfo(args[0]));
using (var finalStream = new MemoryStream())
{
decompressedStream.CopyTo(finalStream);
byte[] decompressedBytes = new byte[finalStream.Length];
}
EDIT: Somebody wanted me to add the code for Decompress() so here it is
public static GZipStream Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length) + " (decompressed)";
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
return decompressionStream;
}
}
}
}
The issue is here:
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
return decompressionStream;
}
using statement (see this article) disposes the stream and you can't use it outside of the block. Move your MemoryStream handling inside this block and return byte[] from the method.
Something like this should work:
public static byte[] Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length) + " (decompressed)";
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
using (var finalStream = new MemoryStream())
{
decompressionStream.CopyTo(finalStream);
return finalStream.ToArray();
}
}
}
}

Download byte array from stream writer

Hi I am trying to make working this piece of code, after the copy of the word file template into a memory stream, read it and replace some text, I would convert the stream writer to byte array which will be used to download the result. Thanks in advance
public byte[] GetWordFile()
{
try
{
string sourceFile = Path.Combine("C:/[...]/somefile.docx");
using (MemoryStream inStream = new MemoryStream())
{
using (Stream fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
{
fs.CopyTo(inStream);
}
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(inStream, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
docText = docText.Replace("numpol", "HAHAHHAHA");
using (MemoryStream outStream = new MemoryStream())
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
sw.Flush();
sw.BaseStream.CopyTo(outStream);
outStream.Position = 0;
return outStream.ToArray();
}
}
}
}
catch (Exception ex)
{
///...
}
}

How to Download, Decompress and Deserialize an object/file from Azure Blob Storage?

This code will compress and serialize the object:
public static byte[] ObjectToByteArray(object[] obj)
{
using (MemoryStream msCompressed = new MemoryStream())
using (GZipStream gZipStream = new GZipStream(msCompressed, CompressionMode.Compress))
using (MemoryStream msDecompressed = new MemoryStream())
{
new BinaryFormatter().Serialize(msDecompressed, obj);
byte[] byteArray = msDecompressed.ToArray();
gZipStream.Write(byteArray, 0, byteArray.Length);
gZipStream.Close();
return msCompressed.ToArray();
}
}
And the following will upload it to the Azure Blob Storage:
byte[] byteObject = ObjectToByteArray(uploadObject);
using (Stream stream = new MemoryStream(byteObject))
{
stream.Seek(0, SeekOrigin.Begin);
blockBlob.UploadFromStream(stream, null, options);
}
This works great, but I can't find a way to download, decompress and deserialize this object/file from my storage.
You could use method DownloadToStream to download the file to local.
using (var fileStream = System.IO.File.OpenWrite(#"xxxx\compressedfile.gz"))
{
blockBlob.DownloadToStream(fileStream);
}
And then you could refer to the following code to decompress and deserialize the specified stream.
public static void DecompressAndDeserialize(string path)
{
using (FileStream originalFileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
FileInfo fileToDecompress = new FileInfo(path);
string FileName = fileToDecompress.FullName;
string newFileName = FileName.Remove(FileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
FileStream fs = new FileStream(newFileName, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
object[] uploadObject = (object[])formatter.Deserialize(fs);
}
}

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