brendan-duncan / archive

Dart library to encode and decode various archive and compression formats, such as Zip, Tar, GZip, ZLib, and BZip2.
MIT License
395 stars 130 forks source link

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

Open valerauko opened 5 months ago

valerauko commented 5 months ago

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.

brendan-duncan commented 5 months ago

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.

valerauko commented 5 months ago

That looks fantastic!

brendan-duncan commented 5 months ago

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.

brendan-duncan commented 5 months ago

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.

brendan-duncan commented 5 months ago

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.