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

相关推荐
Zender Han2 小时前
Flutter 视频播放器——flick_video_player 介绍与使用
android·flutter·ios·音视频
恋猫de小郭6 小时前
Flutter Riverpod 3.0 发布,大规模重构下的全新状态管理框架
android·前端·flutter
RaidenLiu8 小时前
Riverpod 3:重建控制的实践与性能优化指南
前端·flutter
蒋星熠1 天前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
卢叁1 天前
Flutter之自定义TabIndicator
前端·flutter
萧雾宇1 天前
Android Compose打造仿现实逼真的烟花特效
android·flutter·kotlin
拜无忧1 天前
【教程】flutter常用知识点总结-针对小白
android·flutter·android studio
拜无忧1 天前
【教程】Flutter 高性能项目架构创建指南:从入门到高性能架构
android·flutter·android studio
醉过才知酒浓1 天前
flutter 拦截返回按钮的方法(WillPopScope or PopScope)
flutter
傅里叶1 天前
sudo启动Flutter程序AMD初始化失败
linux·flutter