archive
archive copied to clipboard
How do I make an archive and send it over the internet without creating a file? On the fly.
How do I make an archive and send it over the internet without creating a file? On the fly.
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 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.
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)
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>.
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).