Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Zip & Unzip

Hello i want to ask how to zip and unzip become string in flutter :

Example :

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();
like image 882
Evan Laksana Avatar asked Apr 30 '26 17:04

Evan Laksana


1 Answers

You can use archive plugin to zip and unzip files.

To unzip:

// 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);
  }
}

To create a zip file:

// Zip a directory to out.zip using the zipDirectory convenience method
var encoder = ZipFileEncoder();
encoder.zipDirectory(Directory('out'), filename: 'out.zip');
like image 119
Omatt Avatar answered May 02 '26 20:05

Omatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!