SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Very strange problem with reading file(s) from archive

Last post 11-05-2007 9:44 AM by JohnReilly. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 09-13-2007 11:57 PM

    • GDragoN
    • Top 200 Contributor
    • Joined on 01-07-2006
    • Serbia & Montenegro
    • Posts 10

    Very strange problem with reading file(s) from archive

    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
  • 09-14-2007 10:42 AM In reply to

    Re: Very strange problem with reading file(s) from archive

    I suspect the ZipFile is being disposed which will close the underlying stream.

    You should keep a reference to the ZipFile alive somewhere. You could set IsStreamOwner for the ZipFile instance to false to stop it closing in another way but this is dodgy for several reasons and isnt recommended.  You lose the ability to control the lifetime of the open stream for one, keeping a reference is simpler and cleaner.

    hth,

    -jr-

     

     

  • 09-14-2007 4:49 PM In reply to

    • GDragoN
    • Top 200 Contributor
    • Joined on 01-07-2006
    • Serbia & Montenegro
    • Posts 10

    Re: Very strange problem with reading file(s) from archive

    It couldn't be a problem, because the problem occures randomly and always during the reading, and when code starts reading from the stream it just closes by itself.
  • 09-14-2007 10:24 PM In reply to

    Re: Very strange problem with reading file(s) from archive

    Hi,

    That is consistent with the facts.  It will only ever occur when reading as thats when the closed stream is accessed.  Its fairly random as the stream is closed when garbage collection takes place.

    hth, -jr-

  • 11-02-2007 1:38 PM In reply to

    • jcm
    • Not Ranked
    • Joined on 11-02-2007
    • Posts 4

    Re: Very strange problem with reading file(s) from archive

    I have exactly the same problem !

    As JR suggested, it should be due to garbage collection (especially since I unzip more than 1 Gb of zip files), but why doesn't that appear with 0.84 ?


    Also, I checked and all the streams are correctly closed.

    The problem appears on zip files with size ranging from 17Mb to 260Mb, containing a lot of small files (mostly 32Kb PDF and 30Kb JPEG)

     

  • 11-05-2007 9:44 AM In reply to

    Re: Very strange problem with reading file(s) from archive

    Hi,

    The streams cannot be correctly closed or there wouldnt be a problem :-).

    Debugging this is likely to be the fastest way to a solution if thats possible.

    hth, -jr-

Page 1 of 1 (6 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.