0

I am trying to read data from a Password Protected Zip file. The password of Demo.Zip is 12345 and I want to read the content inside Demo.zip which is a text file having to contain comma-separated values

I have tried reading the text.zip file which does not have any password and I can retrieve the Data from the text.txt. So, Is there a way to retrieve data from the Password protected Demo.zip knowing the password is 12345?

using System;
using System.IO;
using System.Collections.Generic;
using System.IO.Compression;
using Ionic.Zip;

namespace CSVDemoApplication
{
    class Program
    {
        Dictionary<string, float> CSVReader = new Dictionary<string, float>();

        public void ReadCSVFile(string path)
        {
            using (Ionic.Zip.ZipFile archive = new Ionic.Zip.ZipFile(path))
            {
                archive.Password = "12345";
                archive.Encryption = EncryptionAlgorithm.WinZipAes128;

                foreach (ZipEntry entry in archive)
                {
                    string[] read;
                    char[] seperators = { ',' };
                    StreamReader reader = new StreamReader(entry.OpenReader("12345"));
                    string data = "";

                    while ((data = reader.ReadLine()) != null)
                    {
                        read = data.Split(seperators, StringSplitOptions.None);
                        var newKey = read[0];
                        var newValue = float.Parse(read[1]);
                        CSVReader[newKey] = newValue;
                    }
                }
            }
        }

        public void PrintCSVValues()
        {
            foreach (KeyValuePair<string, float> item in CSVReader)
            {
                Console.WriteLine(item.Key + " - " + item.Value);
            }

        }

        static void Main(string[] args)
        {
            Program p = new Program();
            string path = @"D:\Projects\VS Projects\2019\CSharp\CSVDemoApplication\Demo.zip";
            p.ReadCSVFile(path);
            p.PrintCSVValues();
            Console.ReadLine();
        }
    }
}
  • 1
    Possible duplicate of [Decompressing password-protected ZIP files with .NET 4.5](https://stackoverflow.com/questions/13160490/decompressing-password-protected-zip-files-with-net-4-5) – Heretic Monkey Oct 24 '19 at 13:46
  • Note that [searching Stack Overflow for `"[c#] password-protected zip file"`](https://stackoverflow.com/search?q=%5Bc%23%5D+password+protected+Zip+File) yields quite a few matches other than the one I found... – Heretic Monkey Oct 24 '19 at 13:49
  • Hi, I know there are ways to Extract/Decompress the files and read them, Is there a way to read the content of the files inside a password-protected zip file without Extracting/Decompressing it? –  Oct 25 '19 at 04:09
  • @slalith I think it depends on the zip archive. Some zips have the "central archive header" encrypted, which means you can't know what files are inside without decrypting it, however, in other zip archives, only certain files might encrypted (or each file might be encrypted independently), and therefore the central archive header will be readable and you should be able to determine what the filenames are (and how large they are etc) without a password. – dreamlax Oct 25 '19 at 04:16
  • How would you read the content of a compressed file without decompressing it? Password-protected or not? The compression algorithm takes the bytes of the file to be compressed and rearranges them in a more concise way, so if you were to read them without decompressing them, they would just be an array of bytes with no meaning. – Heretic Monkey Oct 25 '19 at 12:58

0 Answers0