我有以下文件结构:
yyyy.zip
12x MM-yyyy.zip
each of MM-yyyy.zip
28-31x yyyyMMdd.prn
在这种情况下,prn就像txt。
我有一个月的路径,并提取MM-yyyy.zip很好。 不幸的是,yyyy.zip的解包函数不想迭代MM-yyyy.zip并解压缩它们。 (错误如下)
为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;
}
此功能不适用于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;
}
抛出错误():
An unhandled exception of type 'Ionic.Zip.BadReadException' occurred in Ionic.Zip.dll
Additional information: Could not read block - no data! (position 0x00000000)
您的测试的一些数据(目录中不存在07-2013.zip) 如果您想要所有可能的文件,请查看此(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);
和功能
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()));
}
}
修改
在dotNetZip 1.9.3和1.9上,错误是错误的。