I have a task to embed a Zipped PDF retrieved from the file system into an XML document in C#. The specification states:
For instance, retrieve the file ‘INV001.pdf’
Compress it to get a ‘INV001.zip’ file.
2/ Encode each .zip to Base64 format
The goal is to transform the zip file, which is a binary object, into a text file.
The content of the text file will be a text String which we will be able to copy/paste into the .xml file
This is where I've got to with the code
string docText = null;
using (FileStream originalFileStream = File.Open(myFilePath, FileMode.Open))
{
using (FileStream compressedFileStream = File.Create("compressed.gz"))
{
using (var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress, true))
{
originalFileStream.CopyTo(compressor);
using (StreamReader sr = new StreamReader(compressor))
{
docText = sr.ReadToEnd();
}
}
}
}
The code throws an exception on the line
using (StreamReader sr = new StreamReader(compressor))
"Stream was not readable". Am I on the right track here? There is no requirement to store the ZIP, only to paste it as Base64 text into an XML file. I've found some articles on SO that hint at some approaches but none seem to work! Many thanks