Flutter 压缩和解压缩
Posted
技术标签:
【中文标题】Flutter 压缩和解压缩【英文标题】:Flutter Zip & Unzip 【发布时间】:2019-01-29 14:55:25 【问题描述】:你好我想问一下如何在flutter中压缩和解压缩成为字符串:
例子:
final int BUFFER_SIZE = 40;
ByteArrayInputStream is = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) string.append(new String(data, 0, bytesRead));
gis.close();
is.close();
return string.toString();
【问题讨论】:
你能澄清一下这个问题吗? 您能试试看this SO post 是否有帮助吗?让社区understand您的问题很好,以提高您获得答案的机会。 【参考方案1】:您可以使用archive 插件来压缩和解压缩文件。
解压:
// Read the Zip file from disk.
final bytes = File('test.zip').readAsBytesSync();
// Decode the Zip file
final archive = ZipDecoder().decodeBytes(bytes);
// Extract the contents of the Zip archive to disk.
for (final file in archive)
final filename = file.name;
if (file.isFile)
final data = file.content as List<int>;
File('out/' + filename)
..createSync(recursive: true)
..writeAsBytesSync(data);
else
Directory('out/' + filename).create(recursive: true);
要创建一个 zip 文件:
// Zip a directory to out.zip using the zipDirectory convenience method
var encoder = ZipFileEncoder();
encoder.zipDirectory(Directory('out'), filename: 'out.zip');
【讨论】:
以上是关于Flutter 压缩和解压缩的主要内容,如果未能解决你的问题,请参考以下文章