简体   繁体   English

Windows在zip档案中动态存储添加文件

[英]windows store adding files in zip archieve dynamically

I have got a code from this link: 我从此链接获得了代码:

http://www.codeproject.com/Tips/515704/Archive-Multiple-Files-In-Zip-Extract-Zip-Archive http://www.codeproject.com/Tips/515704/Archive-Multiple-Files-In-Zip-Extract-Zip-Archive

that compresses and extracts a file in zip format. 压缩并提取zip格式的文件。

However the compressing part in the code just creates an empty zipfile, so how can i add files programmatically in this zip archieve? 但是,代码中的压缩部分只是创建一个空的zipfile,所以如何在此zip档案中以编程方式添加文件?

i have checked the doc for the ziparchieve class and it has a method for .net called CreateEntryFromFile(String, String) , however this method doesn't apply for .net windows store version. 我已经检查了ziparchieve类的文档,并且它具有.net名为CreateEntryFromFile(String,String)的方法,但是该方法不适用于.net Windows存储版本。

this is the code we are concerned with: 这是我们关注的代码:

private async void ZipClick(object sender, RoutedEventArgs e)
{
FileSavePicker picker = new FileSavePicker();
picker.FileTypeChoices.Add("Zip Files (*.zip)", new List<string> { ".zip" });
picker.SuggestedStartLocation = PickerLocationId.Desktop;
picker.SuggestedFileName = "1";
zipFile = await picker.PickSaveFileAsync();

using (var zipStream = await zipFile.OpenStreamForWriteAsync())
{
    using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
    {
        foreach (var file in storeFile)
        {
            ZipArchiveEntry entry = zip.CreateEntry(file.Name);

            using (Stream ZipFile = entry.Open())
            {
                byte[] data = await GetByteFromFile(file);
                ZipFile.Write(data, 0, data.Length);
            }
        }
    }
}
}

I misunderstood the sample code, it actually can archive the the files you pass it for the first time, but i'm not sure if the same method can be used to add to add other files in the same zip file later. 我误解了示例代码,它实际上可以将您第一次通过的文件存档,但是我不确定以后是否可以使用相同的方法来添加其他文件到同一zip文件中。

  public async void zipit(StorageFile zipFile, StorageFile file)
    {
     using (var zipToOpen = await zipFile.OpenStreamForWriteAsync())
        {
            using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
            {
                ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Name);
                using (Stream writer = readmeEntry.Open())
                {
                    //writer.WriteLine("Information about this package.");
                    //writer.WriteLine("========================");
                    byte[] data = await GetByteFromFile(file);
                    writer.Write(data, 0, data.Length);
                }
            }
        }
    } 

where "zipFile" is the file you have chose to archive in (destination ) and "file" is the original non zipped file. 其中“ zipFile”是您选择在(目的地)中存档的文件,“ file”是原始的非压缩文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM