前言
很簡(jiǎn)單的一個(gè) zip
包解壓縮的功能,但是 windows
平臺(tái)中文顯示亂碼,很糟心,搜了一圈沒(méi)找到現(xiàn)成的方法,在此貼上我的解決方式。
實(shí)現(xiàn)
導(dǎo)入需要的包
flutter pub add archive
flutter pub add fast_gbk
flutter pub add path
代碼如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-648244.html
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 包直接用會(huì)亂碼,這里加一下對(duì)于 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
,若是代碼有問(wèn)題還望指出,非常感謝!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-648244.html
到了這里,關(guān)于flutter 解壓 zip 中文亂碼問(wèn)題處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!