flutter 解压 zip 中文乱码问题处理

前言

很简单的一个 zip 包解压缩的功能,但是 windows 平台中文显示乱码,很糟心,搜了一圈没找到现成的方法,在此贴上我的解决方式。

实现

导入需要的包

复制代码
flutter pub add archive

flutter pub add fast_gbk

flutter pub add path

代码如下:

dart 复制代码
import 'dart:io';
import 'package:fast_gbk/fast_gbk.dart';
import 'package:archive/archive.dart';
import 'package:path/path.dart' as p;

void main() {
  unzip('G:/testUpdate/111.zip', 'G:/testUpdate/1');
}

void unzip(String inputPath, String outputPath) {
  var archive = zipDecode(inputPath);

  for (final file in archive) {
    final filename = file.name;
    final filePath = p.join(outputPath, filename);

    if (!file.isFile && !file.isSymbolicLink) {
      Directory(filePath).createSync(recursive: true);
      continue;
    }

    if (file.isSymbolicLink) {
      final link = Link(filePath);
      link.createSync(p.normalize(file.nameOfLinkedFile), recursive: true);
    } else {
      final data = file.content as List<int>;
      final newFile = File(filePath);
      newFile.createSync(recursive: true);
      newFile.writeAsBytesSync(data);
    }
  }
}

// 由于 archive 包直接用会乱码,这里加一下对于 gbk 编码的处理
Archive zipDecode(String inputPath) {
  final zipFile = File(inputPath);
  final bytes = zipFile.readAsBytesSync();
  final inputStream = InputStream(bytes);
  // final inputStream = InputFileStream('G:/testUpdate/111.zip');
  var directory = ZipDirectory.read(inputStream);

  final archive = Archive();

  for (final zfh in directory.fileHeaders) {
    final zf = zfh.file!;

    // The attributes are stored in base 8
    final mode = zfh.externalFileAttributes!;
    final compress = zf.compressionMethod != ZipFile.STORE;

    //dynamic content = zf.rawContent;
    var file = ArchiveFile(
        zf.filename, zf.uncompressedSize!, zf, zf.compressionMethod);

    file.mode = mode >> 16;

    // see https://github.com/brendan-duncan/archive/issues/21
    // UNIX systems has a creator version of 3 decimal at 1 byte offset
    if (zfh.versionMadeBy >> 8 == 3) {
      file.isFile = false;

      final fileType = file.mode & 0xF000;
      switch (fileType) {
        case 0x8000:
        case 0x0000: // No determination can be made so we assume it's a file.
          file.isFile = true;
          break;
        case 0xA000:
          file.isSymbolicLink = true;
          break;
        default:
      }
    } else {
      file.isFile = !file.name.endsWith('/');
    }

    file.crc32 = zf.crc32;
    file.compress = compress;
    file.lastModTime = zf.lastModFileDate << 16 | zf.lastModFileTime;

    final needGbkDecode = zf.flags & 2048 == 0;
    if (needGbkDecode) {
      file.name = gbk.decode(zf.filename.codeUnits);
    }

    archive.addFile(file);
  }

  return archive;
}

昨天刚接触的 flutter,若是代码有问题还望指出,非常感谢!

相关推荐
IntMainJhy2 分钟前
Flutter 三方库 SecureStorage 加密存储鸿蒙化适配与实战指南(加密读写+批量操作全覆盖)
flutter·华为·harmonyos
2301_814809865 分钟前
实战分享Flutter Web 开发:解决跨域(CORS)问题的终极指南
前端·flutter
程序员老刘15 小时前
为什么满帧运行的游戏,玩起来反而觉得卡顿?
flutter·客户端
猫山月16 小时前
Flutter路由演进路线(2026)
前端·flutter
悟空瞎说18 小时前
Flutter热更新 Shorebird CodePush 原理、实现细节及费用说明
前端·flutter
Lanren的编程日记21 小时前
Flutter 鸿蒙应用AR功能集成实战:多平台AR框架+模拟模式,打造增强现实体验
flutter·ar·harmonyos
zhangjikuan891 天前
Flutter备忘
flutter
Lanren的编程日记1 天前
Flutter 鸿蒙应用启动速度优化实战:优先级并行初始化+懒加载,全方位提升启动体验
flutter·华为·harmonyos
Lanren的编程日记1 天前
Flutter 鸿蒙应用权限管理功能实战:标准化权限申请与状态管控,提升用户信任度
flutter·华为·harmonyos
Lanren的编程日记1 天前
Flutter 鸿蒙应用语音识别功能集成实战:多平台框架+模拟模式,实现便捷语音输入
flutter·语音识别·harmonyos