mardi 19 août 2014

How do I extract an entry from a ZipArchive in Windows Phone 8.1?


Vote count:

0




I'm trying to make a very simple comic viewer app for Windows Phone 8.1 for fun. However, I'm having an issue with instantiating the ZipArchive object.


Here's exactly what I'm trying to do:



  1. User taps a button which opens a FileOpenPicker

  2. User selects a zip file (*.cbr or *.cbz)

  3. The chosen file is saved to the FutureAccessList

  4. A button is added to the screen for the file the user picked (the button's Tag is set to the Token returned from the FutureAccessList)

  5. The user taps the button that was added

  6. The app navigates to another page and sends the token for the file

  7. The receiving page's NavigationHelper_LoadState method receives the token in NavigationParameter

  8. I load the zip file using the token, extract the first entry in the zip file to a temporary file and then show it in an Image control


Steps 1-7 already work. However, I cannot, even after hours of tinkering, figure out why I can't open the zip file. All my problems are caused when I instantiate the ZipArchive object.


When I attempt to do so, my app hangs. I receive no exception. I assume this has something to do with the asynchrony of the way all the file access works but, regardless of how many examples I find online and copy, I cannot figure out what to do.


It is worth noting that the entire code works when I remove everything to do with the Zip file and instead force the user to select an image. In that case, everything works and is easy to understand. It's only when I start interacting with Zip files that my mind turns to goo.


Here is the relevant code starting with the entry point in NavigationHelper_LoadState where I'm grabbing the FutureAccessList file token.



private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
string token = e.NavigationParameter as string;
LoadFile(token);
}

async Task LoadFile(string token)
{
if (!StorageApplicationPermissions.FutureAccessList.ContainsItem(token))
return;

var sf = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);

// Extract an entry from the zip file to the temp folder
await UnzipAndDisplayComic(sf);

// load the temp image from the temp folder and set it as source for the image control
imgViewer.Source = await MakeImage(await
ApplicationData.Current.TemporaryFolder.GetFileAsync("currentComicImage.jpg"));
}

async private Task UnzipAndDisplayComic(StorageFile sf)
{
using (var s = await sf.OpenStreamForReadAsync())
{
// the following line is where my application completely blocks
using (ZipArchive zip = new ZipArchive(s))
{
var entry = zip.Entries.First();
using (Stream firstEntryStream = entry.Open())
{
var tempFolder = ApplicationData.Current.TemporaryFolder;
var tempFile = await tempFolder.CreateFileAsync("currentComicImage.jpg", CreationCollisionOption.ReplaceExisting);

using (var tempFileStream = await tempFile.OpenStreamForWriteAsync())
{
await firstEntryStream.CopyToAsync(tempFileStream);
}
}
}
}
}

async Task<ImageSource> MakeImage(StorageFile file)
{
BitmapImage bitmapImage = null;

if (file != null)
{
bitmapImage = new BitmapImage();

using (var stream = await file.OpenReadAsync())
{
await bitmapImage.SetSourceAsync(stream);
}
}
return (bitmapImage);
}


What exactly do I have to do in order to fix this issue with my application being blocked when I instantiate the ZipArchive object?



asked 34 secs ago







How do I extract an entry from a ZipArchive in Windows Phone 8.1?

Aucun commentaire:

Enregistrer un commentaire