brendan-duncan/archive

recommended conversion

Closed this issue · 3 comments

I'm trying to write a client app that downloads and processes .zip or .bz2 files (created by 7-zip).

How should the downloaded String be converted to the List int that BZip2Decoder or ZipDecoder expect?

Thanks

It depends on how you're getting the file. If you're using HttpRequest, then one way I found that works is:

  var req = new Html.HttpRequest();
  req.open('GET', url+ '/test.zip'); // Request a zip file
  req.overrideMimeType('text\/plain; charset=x-user-defined');
  req.onLoadEnd.listen((e) {
    if (req.status == 200) {
      // Convert the responseText to a byte array.
      var bytes = req.responseText.split('').map((e) {
        return new String.fromCharCode(e.codeUnitAt(0) & 0xff);
       }).join('').codeUnits;

      // Decode the zip file.
      Arc.Archive archive = new Arc.ZipDecoder().decodeBytes(bytes);
      // Do something with the Arcive
    }
  }

There's probably a better way to convert the responseText to a byte array but I haven't spent time investigating it much yet.

Thanks a lot for your reply. Turned out I neglected to override the mime
type...

BTW, great work you've done there!

Best regards

Alexis

On Sat, Mar 1, 2014 at 8:34 PM, Brendan Duncan notifications@github.comwrote:

It depends on how you're getting the file. If you're using HttpRequest,
then one way I found that works is:

var req = new Html.HttpRequest();
req.open('GET', url+ '/test.zip'); // Request a zip file
req.overrideMimeType('text/plain; charset=x-user-defined');
req.onLoadEnd.listen((e) {
if (req.status == 200) {
// Convert the responseText to a byte array.
var bytes = req.responseText.split('').map((e) {
return new String.fromCharCode(e.codeUnitAt(0) & 0xff);
}).join('').codeUnits;

  // Decode the zip file.
  Arc.Archive archive = new Arc.ZipDecoder().decodeBytes(bytes);
  // Do something with the Arcive
}

}

There's probably a better way to convert the responseText to a byte array
but I haven't spent time investigating it much yet.


Reply to this email directly or view it on GitHubhttps://github.com//issues/5#issuecomment-36432456
.