brendan-duncan/archive

fix: nested `zipDirectory` filename causes zipping corrupted zip

Closed this issue · 4 comments

Description

When using zipDirectory and specifying the filename as a path within the directory, the resulting zipped file will have a corrupted zip inside.

Minimal reproductive sample code

Repository with sample code

import 'dart:io';
import 'package:archive/archive_io.dart';
import 'package:path/path.dart' as path;

void main() {
  final inputDirectory = Directory(
    path.join(Directory.current.path, 'input_zip'),
  )..createSync(recursive: true);
  final outputDirectory = Directory(
    path.join(Directory.current.path, 'output_zip'),
  )..createSync(recursive: true);

  File(path.join(inputDirectory.path, 'file1.txt')).writeAsStringSync('file1');

  const zipFileName = 'output1.zip';
  final zipFilePath = path.join(inputDirectory.path, zipFileName);
  ZipFileEncoder().zipDirectory(inputDirectory, filename: zipFilePath);

  final zipBytes = File(
    zipFilePath,
  ).readAsBytesSync();

  File(
    path.join(outputDirectory.path, 'output2.zip'),
  )
    ..createSync(recursive: true)
    ..writeAsBytesSync(zipBytes);
}

Run the above snippet of code locally and unzip the output's directory zip file. It will have the text file and a corrupted zip.

Proposals

  • Show warning when filename's path is nested within directory.path
  • Avoiding filename being a path and only extract the filename from the filename path
  • Gracefully detect and resolve this scenarios

Additional context

  • I'm willing to work on a solution if we agree this is an issue and settle on how this should be resolved.

I'm trying to figure out what you're doing here, but yes there does seem to be a problem. You're zipping a directory, and the path of the zip file being created is inside the directory you're zipping.

My thought is it should throw an exception in this case.

My thought is it should throw an exception in this case.

I'm happy with this approach! Would you like me to contribute or do you prefer taking care of this?

I'd be glad if you could take care of it, thanks!

Fantastic! Feel free to assign this to me, I will look into making a Pull Request between this week and next week.