I have the following file structure:
yyyy.zip
12x MM-yyyy.zip
each of MM-yyyy.zip
28-31x yyyyMMdd.prn
prn in this case is something like txt.
I have a path to a month and extracting MM-yyyy.zip works great. Unfortunately, unpacking function for yyyy.zip do not want to iterate through MM-yyyy.zip and unpack them. (Error is below)
The working for the MM-yyyy.zip
private static Dictionary<string, MemoryStream> UnZipToMemory(string url)
{
var result = new Dictionary<string, MemoryStream>();
using (var client = new WebClient())
using (ZipFile zip = ZipFile.Read(new MemoryStream(client.DownloadData(url))))
{
foreach (ZipEntry e in zip)
{
MemoryStream data = new MemoryStream();
e.Extract(data);
result.Add(e.FileName, data);
}
}
return result;
}
This function is not working for yyyy.zip
private static Dictionary<string, MemoryStream> UnZipToMemoryYear(string url)
{
var result = new Dictionary<string, MemoryStream>();
using (var client = new WebClient())
using (ZipFile zip = ZipFile.Read(new MemoryStream(client.DownloadData(url))))
{
foreach (ZipEntry e in zip)
{
MemoryStream data = new MemoryStream();
e.Extract(data);
using (ZipFile zi = ZipFile.Read(data)) //error is from this line
{
foreach (ZipEntry f in zi)
{
MemoryStream data2 = new MemoryStream();
f.Extract(data2);
result.Add(f.FileName, data2);
}
}
}
}
return result;
}
Thrown error ():
An unhandled exception of type 'Ionic.Zip.BadReadException' occurred in Ionic.Zip.dll
Additional information: Could not read block - no data! (position 0x00000000)
Some data for your testing (07-2013.zip does not exist in the directory) If you want all possible files, look at this (http://bossa.pl/index.jsp?layout=mstock&page=1&news_cat_id=706&dirpath=/metastock/mstock/sesjaall)
//string url = "http://bossa.pl/pub/metastock/mstock/sesjaall/07-2014.zip";
string url = "http://bossa.pl/pub/metastock/mstock/sesjaall/2013.zip";
//DownloadZipTest(url);
DownloadZipTestYear(url);
And functions
private void DownloadZipTest(string url)
{
foreach (KeyValuePair<string, MemoryStream> x in UnZipToMemory(url))
{
Console.WriteLine("");
Console.WriteLine(x.Key);
Console.WriteLine(Encoding.UTF8.GetString(x.Value.ToArray()));
}
}
private void DownloadZipTestYear(string urlYear)
{
foreach (KeyValuePair<string, MemoryStream> x in UnZipToMemoryYear(urlYear))
{
Console.WriteLine("");
Console.WriteLine(x.Key);
Console.WriteLine(Encoding.UTF8.GetString(x.Value.ToArray()));
}
}
EDIT
On dotNetZip 1.9.3 and 1.9 errors are equeal.