0

我正在以这种方式在我的 UWP 应用程序中下载一些 .zip 文件。

var rootFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("attachment", CreationCollisionOption.OpenIfExists );
        FileInfo fInfo;
        this.infoFiles.TryTake(out fInfo);

            StorageFile coverpic = await rootFolder.CreateFileAsync(fInfo.FileName, CreationCollisionOption.ReplaceExisting);

            try
            {
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                Uri URI = new Uri(fInfo.Url);
                byte[] buffer = await client.GetByteArrayAsync(URI); // Download file
                using (Stream stream = await coverpic.OpenStreamForWriteAsync())
                    stream.Write(buffer, 0, buffer.Length); // Save


                StorageFile zipFile = await MyFileHelper.GetFileAsync(rootFolder, fInfo.FileName);


                if (fInfo.FileName.ToLower().Contains(".zip"))
                {
                    await UnZipFileAsync(zipFile, rootFolder).ConfigureAwait(false);
                    if (Instance.DownloadedNotification != null)
                    {

                        Instance.DownloadedNotification(fInfo.FileName);
                    }
                    if (await rootFolder.TryGetItemAsync("5301.zip") != null)
                    {
                        Debug.WriteLine("Soon after Download and Unzip finished 5301.zip exists");
                    }
                    else
                    {
                        Debug.WriteLine("Soon after Download and Unzip finished 5301.zip doesn't exists");
                    }




                }
                else
                {
                    if (Instance.DownloadedNotification != null)
                    {

                        Instance.DownloadedNotification(fInfo.FileName);
                    }
                    if (await rootFolder.TryGetItemAsync("5301.zip") != null)
                    {
                        Debug.WriteLine("Soon after Download and Unzip finished and file name doesn't contain \"zipFile\" 5301.zip exists");
                    }
                    else
                    {
                        Debug.WriteLine("Soon after Download and Unzip finished and file name doesn't contain \"zipFile\" 5301.zip doesn't exists");
                    }
                }


                // return coverpic;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception Occured--" + e.Message);
            }

我正在异步下载 30 多个文件。我下载 1 个文件并保存。然后解压(其实我是在不解压的情况下读取.zip文件的内容。然后将包含的文件复制到根文件夹)。成功复制后,我正在下载第二个 .zip 文件。

我的问题是,当这个循环进行时,根文件夹中缺少一些已经下载的文件(根文件夹中都缺少.zip 文件和解压缩文件)。例如,假设 5301.zip 文件作为第一个文件下载并解压缩。如果程序正在下载第 10 个文件(5310.zip),则此 5301.zip 文件和解压缩文件均丢失。我的文件正在下载到

C:\Users\myuser\AppData\Local\Packages\de187d8d-my-app-id-2b3b8a255c7b_tqgs61w0frgtm\LocalState\attachment

为什么会发生这种情况?请指导我解决此问题。谢谢!

4

0 回答 0