无法将我的文件保存在zip存档中

时间:2017-12-12 22:18:00

标签: c# uwp

我自己编写了一些代码,用于生成包含其他文件的Zip文件。此文件是从画布创建的。但是创建的zip文件是空的,并且不包含任何内容。也没有例外。知道什么可能是错的吗?

var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
savePicker.FileTypeChoices.Add("Extended sketchpad", new[] { ".exsk" });

Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (null != file)
{
    try
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
            {
                var giffile = archive.CreateEntry("layer.gif");
                using (Stream entryStream = giffile.Open())
                {
                    await MyInkCanvas.InkPresenter.StrokeContainer.SaveAsync(entryStream.AsOutputStream());
                }
            }

            using (Windows.Storage.Streams.IRandomAccessStream finalStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
            {
                finalStream.Size = 0;

                memoryStream.Seek(0, SeekOrigin.Begin);
                memoryStream.CopyTo(finalStream.AsStreamForWrite());
            }
        }
        //MainPage.NotifyUser("File has been saved!", NotifyType.StatusMessage);
    }
    catch (Exception ex)
    {
        //MainPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
    }
}

1 个答案:

答案 0 :(得分:0)

为什么不直接写文件?

using (var fileStream = await file.OpenStreamForWriteAsync())
{
    using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
    {
        var giffile = archive.CreateEntry("layer.gif");

        using (Stream entryStream = giffile.Open())
        {
            await MyInkCanvas.InkPresenter.StrokeContainer.SaveAsync(entryStream.AsOutputStream());
        }
    }
}