brendan-duncan / archive

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

fix: nested `zipDirectory` filename causes zipping corrupted zip #297

Closed alestiago closed 8 months ago

alestiago commented 8 months ago

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

Additional context

brendan-duncan commented 8 months ago

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.

alestiago commented 8 months ago

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?

brendan-duncan commented 8 months ago

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

alestiago commented 8 months ago

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