brendan-duncan/archive

Is it possible to work with dart:io File directly?

Opened this issue · 5 comments

I get dart:io File objects passed to my process from a different library.

Is it possible to utilize that File object directly? It feels roundabout to (re)open it as an InputFileStream.

I'm trying to extract zip archives to disk.

My idea would be like pipeing a HTTP StreamedResponse into a File -- would be nice if I could "pipe" an ArchiveFile into an io File.

I just added a RandomAccessFile constructor to the FileHandle class, in the github version.

I'll still play around with the API to simplify it, but with that constructor I added a unit test as

    final fp = File('$testDirPath/res/zip/zip_bzip2.zip');
    final raf = await fp.open();
    final fh = FileHandle.from(raf);
    final fb = FileBuffer(fh);
    
    final fs = InputFileStream.withFileBuffer(fb);
    expect(fs.readByte(), equals(80));

I could then add a RandomAccessFile (or File, which would be opened inside the class) constructor to InputFileStream to consolidate all that.

That looks fantastic!

There's always some complication to simplifying things.

InputFileStream can't have a direct import of dart:io to take a File constructor input, so I can't simplify it there.
FileBuffer is the same.
dart:io was recently removed from them so they could be used in html places.

I'll have to think about ways to simplify the API for this some more.

I suppose I could take out one layer by having InputFileStream take a FileHandle, in which case it would just be wrapping it in a FileBuffer instead of having you do that.

Something like

    final fp = File('$testDirPath/res/zip/zip_bzip2.zip');
    final fh = FileHandle.fromFile(fp);   
    final fs = InputFileStream.withFileHandle(fh);

At least it's a few less lines.

Ok, I pushed that last thought to github. You can set it up either way now. It might be a little bit before I can get to do another release.