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

How do I check whether zip file is password protected? #259

Open imoreapps opened 1 year ago

imoreapps commented 1 year ago

First of all, thank you for providing such a great Flutter package. As shown in the title, how do I check whether zip file is password protected? Any tips would be appreciated.

brendan-duncan commented 1 year ago

Unfortunately there isn't a good (any) way currently.

imoreapps commented 1 year ago

Unfortunately there isn't a good (any) way currently.

I've stripped a temporary method from the archive package to do this.

bool isZipFileEncrypted(File zipFile) {
  final input = InputFileStream(zipFile.path);
  try {
    final signature = input.readUint32();
    if (signature != ZipFile.SIGNATURE) {
      throw ArchiveException('Invalid Zip Signature');
    }
    final version = input.readUint16();
    final flags = input.readUint16();
    final encryptionType = (flags & 0x1) != 0
        ? ZipFile.encryptionZipCrypto
        : ZipFile.encryptionNone;
    return encryptionType != ZipFile.encryptionNone;
  } finally {
    input.close();
  }
}