brendan-duncan / archive

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

.tar.gz encode error #284

Open BppleMan opened 11 months ago

BppleMan commented 11 months ago

archive version: 3.4.2

// with flutter_test

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  test('temp', tempTest);
}

Future<void> tempTest() async {
  var baseDir = Directory.systemTemp.createTempSync('demo_archive');
  /*
  source
  ├── child_dir
  │   ├── child_file_1
  │   └── child_file_2
  └── child_file
  */
  var sourceDir = Directory(join(baseDir.path, 'source'));
  sourceDir.createSync();
  var childFile = File(join(sourceDir.path, 'child_file'));
  childFile.createSync();
  var childDir = Directory(join(sourceDir.path, 'child_dir'));
  childDir.createSync();
  var childFile1 = File(join(childDir.path, 'child_file_1'));
  childFile1.createSync();
  var childFile2 = File(join(childDir.path, 'child_file_2'));
  childFile2.createSync();

  var destPath = join(baseDir.path, 'dest.tar.gz');
  await TarFileEncoder().tarDirectory(sourceDir,
      compression: TarFileEncoder.GZIP, filename: destPath);
  await extractFileToDisk(destPath, join(baseDir.path, 'dest'));

  expect(
    true,
    File(join(baseDir.path, 'dest', 'source')).existsSync(),
  );
  expect(
    true,
    File(join(baseDir.path, 'dest', 'source', 'child_file')).existsSync(),
  );
  expect(
    true,
    Directory(join(baseDir.path, 'dest', 'source', 'child_dir')).existsSync(),
  );
  expect(
    true,
    File(join(baseDir.path, 'dest', 'source', 'child_dir', 'child_file_1'))
        .existsSync(),
  );
  expect(
    true,
    File(join(baseDir.path, 'dest', 'source', 'child_dir', 'child_file_2'))
        .existsSync(),
  );
}

run it with flutter_test, got error

dart:io                                                     _File.createSync
package:archive/src/io/output_file_stream.dart 27:10        new OutputFileStream
package:archive/src/io/extract_archive_to_disk.dart 181:24  extractFileToDisk

FileSystemException: Creation failed, path = '/var/folders/k4/877lnnqs0wg9lsvfkxyz8j500000gn/T/demo_archiveE0796p/dest/source/child_dir' (OS Error: Not a directory, errno = 20)

when I use other compress tool(eg: keka) encode the source dir, extractFileToDisk can correctly decode tar.gz files.

source/child_dir/ will be recognized as file, so when extracting, traversing archive.files will directly create source/child_dir as File instead of a Directory

BppleMan commented 11 months ago

I created a PR to fix the problem

288

brendan-duncan commented 11 months ago

Thanks for the fix!