Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions lib/import/import_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@

import 'dart:async';
import 'dart:io';
import 'dart:io' as io show ZLibDecoder;
import 'dart:isolate';
import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:archive/archive.dart' hide ZLibDecoder;
import 'package:path/path.dart' as p;
import 'package:sqflite/sqflite.dart';

Expand Down Expand Up @@ -636,7 +637,48 @@ Future<void> _inflateMember(String zipPath, int index, String destPath) =>
Isolate.run(() async {
final input = InputFileStream(zipPath);
try {
final f = ZipDecoder().decodeStream(input).files[index];
final dec = ZipDecoder();
final f = dec.decodeStream(input).files[index];
ZipFileHeader? h;
for (final x in dec.directory.fileHeaders) {
if (x.filename == f.name) {
h = x;
break;
}
}
// archive 4.0.9's `writeContent` gathers every inflated chunk in memory
// and writes at the end (a 909 MB database peaked ~1.1 GB RSS, #1042).
// Stream the raw deflate straight to disk instead. No size cap here, so
// a stale zip size field still imports; the caller's CRC check is the
// integrity gate. Anything unusual falls through to `writeContent`.
if (h != null &&
(h.compressionMethod == 0 || h.compressionMethod == 8) &&
(h.generalPurposeBitFlag & 1) == 0) {
final raf = await File(zipPath).open();
final int start;
try {
await raf.setPosition(h.localHeaderOffset);
final lh = ByteData.sublistView(await raf.read(30));
if (lh.lengthInBytes == 30 &&
lh.getUint32(0, Endian.little) == 0x04034b50) {
start = h.localHeaderOffset +
30 +
lh.getUint16(26, Endian.little) +
lh.getUint16(28, Endian.little);
} else {
start = -1;
}
} finally {
await raf.close();
}
if (start >= 0) {
Stream<List<int>> s =
File(zipPath).openRead(start, start + h.compressedSize);
if (h.compressionMethod == 8) s = s.transform(io.ZLibDecoder(raw: true));
await s.pipe(File(destPath).openWrite());
return;
}
}
final sink = OutputFileStream(destPath);
try {
f.writeContent(sink);
Expand Down
Loading