8

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();
Melvin
  • 925
  • 1
  • 10
  • 22
Evan Laksana
  • 410
  • 3
  • 7
  • 17
  • can you clarify the question? – Harsh Bhikadia Apr 05 '21 at 09:28
  • Could you try to check if [this SO post](https://stackoverflow.com/questions/39735145/how-to-compress-a-string-using-gzip-or-similar-in-dart) could help? Let the community [understand](https://stackoverflow.com/help/how-to-ask) your issue well to improve your chances of getting an answer. – MαπμQμαπkγVπ.0 Jul 30 '21 at 14:46
  • Does this answer your question? [How to compress a string using GZip or similar in Dart?](https://stackoverflow.com/questions/39735145/how-to-compress-a-string-using-gzip-or-similar-in-dart) – Vega Jun 11 '22 at 05:56

1 Answers1

4

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');
Omatt
  • 8,564
  • 2
  • 42
  • 144