Hi,
I have an archive with some files in it. I want to use files from the archive directly without unziping them. I want to read files as streams. I use following code:
--------------------------
public static Stream GetFileStreamFromArchive(string ArchivePath, string FileName)
{
ZipFile zip = new ZipFile(ArchivePath);
ZipEntry theEntry = zip.GetEntry(FileName);
return zip.GetInputStream(theEntry);
}
public static ArrayList GetFileArrayListFromArchive(string ArchivePath, string FileName)
{
Stream file = GetFileStreamFromArchive(ArchivePath, FileName);
ArrayList Response = GetFileArrayListFromStream(file);
file.Close();
return Response;
}
public static ArrayList GetFileArrayListFromStream(Stream InputStream)
{
ArrayList Response = new ArrayList();
StreamReader sr = new StreamReader(InputStream);
string line;
while ((line = sr.ReadLine()) != null) Response.Add(line.Split('\t'));
sr.Close();
return Response;
}
------------------------
I call 'GetFileArrayListFromArchive' method, ArchivePath is path to the zip file, and FileName is file from the zip archive I want to read. When my program starts, it read few files one by one. Then I need to read another file from some part of the program, I call this same method, and code breaks in 'GetFileArrayListFromStream' method during the ReadLine in while block. And it always breaks on a different line in the file I need. And I get exception sayin: 'Cannot access a closed file.' There is no rule on this error, some times I manage to call this method 10-15 times, and always get correct file contents, and then it just breaks on random line from the file.
1. Why the file is closed during reading?
2. Should I set something to ZipFile object to ensure proper reading?
3. Is there a different, better, safer method to do this?
Regards,
Milan