Android 无法在颤振中提取拉链

Android 无法在颤振中提取拉链,android,ios,flutter,zip,archive,Android,Ios,Flutter,Zip,Archive,正在从服务器下载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

正在从服务器下载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);
  }
}
  }
String\u localZipFileName='archive.zip';
未来下载文件(字符串url、字符串文件名)异步{
//网址==http://115.166.142.178:3000/staff/syncdata
var req=wait http.Client().get(Uri.parse(url));
Log.e(“下载目录”,_dir);
var file=file('$_dir/$fileName');
返回file.writeAsBytes(req.bodyBytes);
}
Future\u downloadZip(字符串\u zipPath)异步{
设置状态(){
_下载=真;
});
Log.e(“Zippath”,_Zippath);
_图像。清除();
_tempImages.clear();
var zippedFile=await _downloadFile(_zipPath,_localZipFileName);
Log.e(“Zippath”,zippedFile.path);
等待unarchiveAndSave(zippedFile);
设置状态(){
_images.addAll(_tempages);
_下载=假;
});
}
unarchiveAndSave(var zippedFile)异步{
var bytes=zippedFile.readAsBytesSync();
var archive=ZipDecoder().decodeBytes(字节);
for(存档中的var文件){
var fileName='$_dir/${file.name}';
if(file.isCompressed){
var outFile=文件(文件名);
Log.e('File::',outFile.path);
//_tempImages.add('${outFile.path}/productImages');
outFile=等待outFile.create(递归:true);
等待outFile.writeAsBytes(file.content);
}
}
}

在Dart中从未尝试过这样做,但我在其他语言中也遇到过类似的问题。 如果您忘记验证正在处理的文件类型,通常会出现此问题。 文件对象应具有布尔变量
file.isFile
file.isDirectory

你可以试着遵循这个

导入'dart:io';
将“package:path/path.dart”作为p导入;
导入“package:path/path.dart”;
导入“package:archive/archive.dart”;
导入“包:归档/归档io.dart”;
var path=p.Context(样式:style.posix);
最后一个字符串_filename=Platform.script.path.replaceFirst(“/”,“”);
最后一个字符串_dirname=目录(_文件名).parent.path;
//从磁盘读取Zip文件
List bytes=File(path.join(_dirname,'test-a-master.zip')).readAsBytesSync();
//解码压缩文件
Archive Archive=ZipDecoder().decodeBytes(字节);
//将Zip压缩存档的内容解压缩到磁盘
用于(存档中的存档文件){
字符串文件名=file.name;
字符串decodePath=path.join(uu dirname,filename);
if(file.isFile){
列表数据=file.content;
文件(解码路径)
..createSync(递归:true)
..writeAsBytesSync(数据);
}否则{
目录(解码路径)…创建(递归:true);
}
}

希望它能帮助您

我担心Dart可能不支持从盒子中提取.zip文件


我发现了一个处理zip提取的包:

ArchiveFile
提供了父zip的文件名。我发现了这个问题。问题是zip包含zip。
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);
    }
  }