请教各位,我在读取压缩文件包中的一个文件时出错.
这句读不出数据流: inputStream = zip.GetInputStream(entry);
代码如下:
public List<ReportInputResult> SubmitReport(System.IO.Stream stream, DateTime reportDuration, Guid reportOrganID, int adminorganType, ReportPeriod period)
{
List<ReportInputResult> resultList = new List<ReportInputResult>(); //从压缩包中读取报表编码 ZipFile zip = new ZipFile(stream);
foreach (ZipEntry entry in zip)
{
if (entry.IsFile && (entry.Name.EndsWith(".xls") || entry.Name.EndsWith(".XLS"))) {
ReportInputResult result = new ReportInputResult();
Stream inputStream;
ExcelDataSheetReader excelSheet;
string reportType;
try//判断上传的文件是否是EXCEL文件
{
inputStream = zip.GetInputStream(entry);
excelSheet = new ExcelDataSheetReader(inputStream, 0);
excelSheet.SkipToRow(1);
excelSheet.SkipToColumn(1);
reportType = excelSheet.Read();
}
catch(Exception ex)
{
result.ReportName = entry.Name;
result.ErrorMessage = "上传失败:文件格式无法识别!" + ex.Message;
result.IsSuccess = false;
resultList.Add(result);
continue;
}
}
}
return resultList;
}
我在调试时有以下信息:“zip.GetInputStream(entry).ReadTimeout”引发了“System.InvalidOperationException”类型的异常
这句读不出数据流: inputStream = zip.GetInputStream(entry);
代码如下:
public List<ReportInputResult> SubmitReport(System.IO.Stream stream, DateTime reportDuration, Guid reportOrganID, int adminorganType, ReportPeriod period)
{
List<ReportInputResult> resultList = new List<ReportInputResult>(); //从压缩包中读取报表编码 ZipFile zip = new ZipFile(stream);
foreach (ZipEntry entry in zip)
{
if (entry.IsFile && (entry.Name.EndsWith(".xls") || entry.Name.EndsWith(".XLS"))) {
ReportInputResult result = new ReportInputResult();
Stream inputStream;
ExcelDataSheetReader excelSheet;
string reportType;
try//判断上传的文件是否是EXCEL文件
{
inputStream = zip.GetInputStream(entry);
excelSheet = new ExcelDataSheetReader(inputStream, 0);
excelSheet.SkipToRow(1);
excelSheet.SkipToColumn(1);
reportType = excelSheet.Read();
}
catch(Exception ex)
{
result.ReportName = entry.Name;
result.ErrorMessage = "上传失败:文件格式无法识别!" + ex.Message;
result.IsSuccess = false;
resultList.Add(result);
continue;
}
}
}
return resultList;
}
我在调试时有以下信息:“zip.GetInputStream(entry).ReadTimeout”引发了“System.InvalidOperationException”类型的异常
System.IO.FileStream fs = new System.IO.FileStream("c:\\123\\123.zip", FileMode.Open);
ZipFile zip = new ZipFile(fs);
foreach (ZipEntry entry in zip)
{
str2 = zip.GetInputStream(entry);
} fs.Close(); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!可能因为你的流没有关闭
//我试了,如果不关闭流很有可能第二次打不开
http://www.icsharpcode.net/OpenSource/SharpZipLib/