Reading and displaying a PDF inside a zipped folder without extracting it

2

Is it possible to do this or do I really have to extract the file before then read it?

In my code I'm doing this:

  string zipPath = @"C:\Users\Analistas\Desktop\ZipFile.zip";

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            var sample = archive.GetEntry("PdfFile.pdf");  
            string pdfContent = ReadPdfFile("PdfFile.pdf");
            if (sample != null)
            {
                using (var zipEntryStream = sample.Open())
                {
                    MessageBox.Show(pdfContent);
                }
            }
        }

And the method to read PDF:

        public string ReadPdfFile(string fileName)
        {
            StringBuilder text = new StringBuilder();

            if (File.Exists(fileName))
            {
                PdfReader pdfReader = new PdfReader(fileName);

                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                {
                    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                    string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

                    currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                    text.Append(currentText);
                }
                pdfReader.Close();
            }
            return text.ToString();
        }
  

Nothing is displayed.

    
asked by anonymous 04.01.2018 / 18:55

1 answer

0

It is possible to read the pdf that is inside a zipped folder without having to extract it. Just instantiate the PdfReader object using the Stream object of the desired file.

var pdfReader = new PdfReader(ZipFileStream);

One suggestion would be to change your code to pass object Stream to method ReadPdfFile

var archive = ZipFile.OpenRead(zipPath);
var sample = archive.GetEntry("PdfFile.pdf");  
var zipEntryStream = sample.Open();

var pdfContent = ReadPdfFile(zipEntryStream);

And the ReadPdfFile method must be changed to read object Stream .

public string ReadPdfFile(Stream fileStream)
{
    ...
    PdfReader pdfReader = new PdfReader(fileStream);
    ...
}
    
04.01.2018 / 20:33