6

I want to compress a string in Dart (in the browser). I tried this:

import 'package:archive/archive.dart';

[...]

List<int> stringBytes = UTF8.encode(myString);
List<int> gzipBytes = new GZipEncoder().encode(stringBytes);
String compressedString = UTF8.decode(gzipBytes, allowMalformed: true);

Obviously UTF8.decode is not intended for this and it doesn't work (file is unreadable).

What is the right way to compress a string in Dart?

stommestack
  • 2,431
  • 7
  • 23
  • 39
  • What are you trying to achieve? I mean what are you trying to do *with* compressed strings? Can you post more of your code, including writing to a file? – Argenti Apparatus Sep 28 '16 at 01:25
  • 2
    https://pub.dartlang.org/packages/archive – Günter Zöchbauer Sep 28 '16 at 05:17
  • @GünterZöchbauer Yes, that's what I am using – stommestack Sep 28 '16 at 09:56
  • Sorry, missed the import. I got the impression you tried it with plain Dart. – Günter Zöchbauer Sep 28 '16 at 09:57
  • 1
    @ArgentiApparatus I encode the string as a data URI and download it, as described here http://stackoverflow.com/a/18197511/1590323 Works perfectly, but I'd like to compress it first. Compressing it manually as gzip decreases size a lot. – stommestack Sep 28 '16 at 09:59
  • What is the uncompressed data in the file and what do you intend to do with it? – Argenti Apparatus Sep 28 '16 at 14:00
  • I have a feeling that you are attempting to save the raw compressed bytes as a file by downloading it from the browser. I think maybe what you actually want to do is create a text file containing your string then put that in a zip *archive* file, and download that. – Argenti Apparatus Sep 28 '16 at 14:08
  • @ArgentiApparatus I'm making a webapp. The string is XML that contains the user's work. It's saved with a custom file extension and can later be opened again. – stommestack Sep 28 '16 at 14:16

3 Answers3

7

The compressed list of bytes is probably not a valid UTF8 sequence instead you could encode it in base64.

import 'dart:convert';
import 'package:archive/archive.dart';

void main() {
  var myString = 'myString';
  var stringBytes = utf8.encode(myString);
  var gzipBytes = GZipEncoder().encode(stringBytes);
  var compressedString = base64.encode(gzipBytes);

  print(compressedString);
}
Xavier
  • 3,148
  • 21
  • 15
5

You can use this function below if you want.

import 'dart:convert';
import 'dart:io';

  void _compress(String json) {
    final enCodedJson = utf8.encode(json);
    final gZipJson = gzip.encode(enCodedJson);
    final base64Json = base64.encode(gZipJson);

    final decodeBase64Json = base64.decode(base64Json);
    final decodegZipJson = gzip.decode(decodeBase64Json);
    final originalJson = utf8.decode(decodegZipJson);
    }
0
String data=' ';
for(int i=0;i<10;i++){
    data=data+'Hello World!\r\n';
}
var original=utf8.encode(data);
var compressed=gzip.encode(original);
var decompressed=gzip.decode(compressed);
  • 2
    Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Jul 04 '21 at 23:42