So, try as I might, I’m having issues decompressing a string in .NET.
The string is being compressed in Javascript with the following:
const compressedFileText = await compressArrayBuffer(await readFileIntoArrayBuffer(selectedFile));
selectedFile is an object defined as
class selectedFile {
readonly file: File;
constructor(file: File) {
}
// remainder omitted...
}
The other functions involved here are:
async function compressArrayBuffer(arrBuffer: ArrayBuffer): Promise<string> {
const stream = new Blob([arrBuffer]).stream();
const compressedReadableStream = stream.pipeThrough(new CompressionStream("gzip"));
const compressedResponse = await new Response(compressedReadableStream);
const blob = await compressedResponse.blob();
tempDownloadFunction(blob);
const buffer = await blob.arrayBuffer();
const bufferView = new Uint8Array(buffer);
const compressedString = new TextDecoder().decode(bufferView);
return compressedString;
}
function tempDownloadFunction(blob: Blob): void {
const elem = window.document.createElement("a");
elem.href = window.URL.createObjectURL(blob);
elem.download = '';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
async function readFileIntoArrayBuffer(selectedFile: SelectedFile): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result as ArrayBuffer);
}
reader.onerror = reject;
reader.readAsArrayBuffer(selectedFile.file);
});
}
This all works fine, and the compressed string is sent to the server as part of a JSON object.
However, when try to decompress on the server I get an Exception with the message:
System.IO.Compression: “The archive entry was compressed using an unsupported compression method.”
I’ve tried decompressing via DeflateStream and GZipStream as below:
public static string Decompress(string compressedString)
{
byte[] decompressedBytes;
byte[] buffer = Encoding.UTF8.GetBytes(compressedString);
var compressedStream = new MemoryStream(buffer);
using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
using (var decompressedStream = new MemoryStream())
{
decompressorStream.CopyTo(decompressedStream);
decompressedBytes = decompressedStream.ToArray();
}
}
return Encoding.UTF8.GetString(decompressedBytes);
}
and…
public static string UnZip(string value)
{
//Transform string into byte[]
byte[] byteArray = new byte[value.Length];
int indexBA = 0;
foreach (char item in value.ToCharArray())
{
byteArray[indexBA++] = (byte)item;
}
//Prepare for decompress
System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Decompress);
//Reset variable to collect uncompressed result
byteArray = new byte[byteArray.Length];
//Decompress
int rByte = sr.Read(byteArray, 0, byteArray.Length);
//Transform byte[] unzip data to string
System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
for (int i = 0; i < rByte; i++)
{
sB.Append((char)byteArray[i]);
}
sr.Close();
ms.Close();
sr.Dispose();
ms.Dispose();
return sB.ToString();
}
I’m not sure what I’m doing wrong, and no searching that I’ve tried has yielded a result. However, taking the compressed string downloaded via the temp function and running it through WinZip correctly decompresses it and yields my input file with all data intact.
Can anyone help, please?