Far*_*ana 5 zip android archive ios flutter
从服务器下载 zip 文件夹并尝试解压 zip,但 zip 有多个目录。我可以下载 zip,但无法解压它。当 zip 有多个图像文件但在我的情况下有多个目录和多个文件时,下面的代码可以工作。
String _localZipFileName = 'archive.zip';
Future<File> _downloadFile(String url, String fileName) async {
//url==http://115.166.142.178:3000/staff/syncdata
var req = await http.Client().get(Uri.parse(url));
Log.e("DOWNLOAD DIRECTORY",_dir);
var file = File('$_dir/$fileName');
return file.writeAsBytes(req.bodyBytes);
}
Future<void> _downloadZip(String _zipPath) async {
setState(() {
_downloading = true;
});
Log.e("Zippath",_zipPath);
_images.clear();
_tempImages.clear();
var zippedFile = await _downloadFile(_zipPath, _localZipFileName);
Log.e("Zippath",zippedFile.path);
await unarchiveAndSave(zippedFile);
setState(() {
_images.addAll(_tempImages);
_downloading = false;
});
}
unarchiveAndSave(var zippedFile) async {
var bytes = zippedFile.readAsBytesSync();
var archive = ZipDecoder().decodeBytes(bytes);
for (var file in archive) {
var fileName = '$_dir/${file.name}';
if (file.isCompressed) {
var outFile = File(fileName);
Log.e('File:: ' , outFile.path);
// _tempImages.add('${outFile.path}/productImages');
outFile = await outFile.create(recursive: true);
await outFile.writeAsBytes(file.content);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 1
从未尝试在 Dart 中这样做,但是我在其他语言中也遇到了类似的问题。如果您忘记验证正在处理的文件类型,通常会出现此问题。文件对象应该有布尔变量file.isFile和file.isDirectory
你可以尝试按照这个例子
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;
// read Zip file from disk
List<int> bytes = File(path.join(__dirname, 'test-a-master.zip')).readAsBytesSync();
// decode Zip file
Archive archive = ZipDecoder().decodeBytes(bytes);
// Extract the contents of Zip compressed archive to disk
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);
}
}
Run Code Online (Sandbox Code Playgroud)
希望对你有帮助
| 归档时间: |
|
| 查看次数: |
5946 次 |
| 最近记录: |