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,若是代码有问题还望指出,非常感谢!

相关推荐
王二蛋与他的张大花10 小时前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
恋猫de小郭17 小时前
Flutter 的FFI/JNI 插件终于能区分不同的 FlutterEngine 了
android·前端·flutter
晚霞的不甘2 天前
Flutter/OpenHarmony 三方库适配实战:google_maps_flutter 地图
flutter·macos·华为·cocoa·harmonyos·1024程序员节
恋猫de小郭2 天前
Flutter HC 旋转 ANR,而且还和之前的 HCPP 问题类似
android·前端·flutter
恋猫de小郭3 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘3 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
恋猫de小郭4 天前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
恋猫de小郭5 天前
Flutter A2UI 1.0 ,原来的 genui 也正式换成了这一套 SDK 了
android·前端·flutter
LinXunFeng5 天前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
好的佩奇5 天前
Dart 之元数据与注解
android·flutter·dart