archive icon indicating copy to clipboard operation
archive copied to clipboard

How do I make an archive and send it over the internet without creating a file? On the fly.

Open ProgrammingLife opened this issue 5 years ago • 5 comments
trafficstars

How do I make an archive and send it over the internet without creating a file? On the fly.

ProgrammingLife avatar Jan 30 '20 04:01 ProgrammingLife

Without using the io library at all, you could create an Archive object, add ArchiveFiles to it, encode it with the ZipEncoder and send the resulting bytes. Something like:

var archive = Archive();
archive.addFile(ArchiveFile('a.txt', 5, 'abcd'.codeUnits));
archive.addFile(ArchiveFile('b.bin', 3, [0, 1, 2]));
var zipBytes = ZipEncoder().encode(archive); // zipBytes will be a list<int> of the compressed zip
// send zipBytes over the internet.

brendan-duncan avatar Jan 30 '20 04:01 brendan-duncan

@brendan-duncan and all, any idea if there is a way to not use files in .addFile(ArchiveFile(..)) for the source data? E.g. use Uint8List or List<int> with the content.

ekuleshov avatar Jan 12 '21 01:01 ekuleshov

The "File" referred to there is a zip archive file, not a disk file. The contents of the zip archive file can be a Uint8List.

final fileContents = Uint8List(...);
ArchiveFile('filename.ext', fileContents.length, fileContents)

brendan-duncan avatar Jan 12 '21 06:01 brendan-duncan

Found it. Thanks.

Guess my confusion with this comes from the Java background. There I can create a ZipOutputStream, essentially a wrapper around any other stream (file or network) and feed all other data to it. Roughly:

ZipOutputStream zos = new ZipOutputStream(someOtherStream);

ZipEntry ze = new ZipEntry(path + file.getName());
anEntry.setMethod(ZipEntry.DEFLATED);
zos.putNextEntry(anEntry);
                
InputStream fis = new FileInputStream(file);
FileUtils.copyStream(fis, zos);
zos.closeEntry();

Note that part writing a ZipEntry content doesn't know anything about Zip. It is just writing to the OutputStream.

I wasn't been able to replicate all that in dart, without loading all data in memory as List<int>.

ekuleshov avatar Jan 12 '21 15:01 ekuleshov

The problem with this Dart implementation is that I wrote it designed for a web implementation, long before Flutter existed, and so it is very much memory centric and not stream centric. I had started adding a io based backend for it, but I don't think I got far enough with it. Maybe someone will take that over some day, clean it up to have a good streaming interface (as long as it doesn't affect the web supporting memory approach).

brendan-duncan avatar Jan 12 '21 17:01 brendan-duncan