Dart: 编码和解码各种存档和压缩格式

import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:path/path.dart';
import 'package:archive/archive.dart';
import 'package:archive/archive_io.dart';

var path = p.Context(style: Style.posix);
final String __filename = Platform.script.path.replaceFirst('/', '');
final String __dirname = Directory(__filename).parent.path;

  // 从磁盘读取Zip文件
  List<int> bytes = File(path.join(__dirname, 'test-a-master.zip')).readAsBytesSync();

  // 解码Zip文件
  Archive archive = ZipDecoder().decodeBytes(bytes);

  // 将Zip存档的内容解压缩到磁盘
  for (ArchiveFile file in archive) {
    String filename = file.name;
    String decodePath = path.join(__dirname, filename);
    if (file.isFile) {
      List<int> data = file.content;
      File(decodePath)
        ..createSync(recursive: true)
        ..writeAsBytesSync(data);
    } else {
      Directory(decodePath)..create(recursive: true);
    }
  }
posted @ 2019-05-23 22:22  Ajanuw  阅读(550)  评论(0编辑  收藏  举报