0

この ( Windows Phone 8 でファイルを解凍する方法) 投稿の UnZipper クラスをアプリで画像付きの zip に使用しましたが、まれにこのエラーが発生する場合があります。

System.Windows.ni.dll System.OutOfMemoryException: 型 'System.OutOfMemoryException' の例外がスローされました。System.Windows.Application.GetResourceStreamInternal(StreamResourceInfo zipPackageStreamResourceInfo、Uri resourceUri) で System.Windows.Application.GetResourceStream(StreamResourceInfo zipPackageStreamResourceInfo、Uri uriResource) で ImperiaOnline.Plugins.UnZipper.GetFileStream(String) で ImperiaOnline.Plugins.IOHelpers.unzip ファイル名(文字列 zipFilePath、文字列 zipDestinationPath)

デバイスには、必要な空きメモリが 2 倍以上あります。誰かがこれを手伝ってくれますか。これが私のコードです:

public static void unzip(string zipFilePath,string zipDestinationPath) {
            using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                var dirNames = isolatedStorage.GetDirectoryNames(zipDestinationPath);
                bool doesFolderExists = (dirNames.Length > 0) ? true : false;
                if (!doesFolderExists)
                {
                    Debug.WriteLine("Folder does not exists");
                    isolatedStorage.CreateDirectory(zipDestinationPath);
                }
                try
                {
                    using (IsolatedStorageFileStream zipFile = isolatedStorage.OpenFile(zipFilePath, FileMode.Open, FileAccess.ReadWrite))
                    {
                        UnZipper unzip = new UnZipper(zipFile);
                        bool isModuleFolderDeleted = false;
                        foreach (string currentFileAndDirectory in unzip.FileNamesInZip())
                        {
                            string[] fileLocations = currentFileAndDirectory.Split('/');
                            string prefix = zipDestinationPath + '/';
                            int locationsCount = fileLocations.Length;
                            string fileName = fileLocations.Last();
                            string currentPath = prefix;
                            for (int i = 0; i < locationsCount - 1; i++)
                            {
                                dirNames = isolatedStorage.GetDirectoryNames(currentPath + fileLocations[i]);
                                doesFolderExists = (dirNames.Length > 0) ? true : false;
                                if (!doesFolderExists)
                                {
                                    isolatedStorage.CreateDirectory(currentPath + fileLocations[i]);
                                    if (i == 2)
                                    {
                                        isModuleFolderDeleted = true;
                                    }
                                }
                                else if (i == 2 && !isModuleFolderDeleted)
                                {
                                    Debug.WriteLine(currentPath + fileLocations[i] + " is deleted and recreated");
                                    DeleteDirectoryRecursively(isolatedStorage, currentPath + fileLocations[i]);
                                    isolatedStorage.CreateDirectory(currentPath + fileLocations[i]);
                                    isModuleFolderDeleted = true;
                                }
                                currentPath += fileLocations[i] + '/';
                            }
                            var newFileStream = isolatedStorage.CreateFile(currentPath + fileName);
                            byte[] fileBytes = new byte[unzip.GetFileStream(currentFileAndDirectory).Length];
                            unzip.GetFileStream(currentFileAndDirectory).Read(fileBytes, 0, fileBytes.Length);
                            unzip.GetFileStream(currentFileAndDirectory).Close();
                            try
                            {
                                newFileStream.Write(fileBytes, 0, fileBytes.Length);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("FILE WRITE EXCEPTION: " + ex);
                                newFileStream.Close();
                                newFileStream = null;
                                zipFile.Close();
                                unzip.Dispose();
                            }
                            newFileStream.Close();
                            newFileStream = null;
                        }
                        zipFile.Close();
                        unzip.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }

                isolatedStorage.DeleteFile(zipFilePath);
            }
        }

このエラーは次の場所に表示されます。

var newFileStream = isolatedStorage.CreateFile(現在のパス + ファイル名);
byte[] fileBytes = new byte[unzip.GetFileStream(currentFileAndDirectory).Length]; unzip.GetFileStream(currentFileAndDirectory).Read(fileBytes, 0, fileBytes.Length);
unzip.GetFileStream(currentFileAndDirectory).Close();

私はそれをデバッグし、それは失敗します

byte[] fileBytes = new byte[unzip.GetFileStream(currentFileAndDirectory).Length];

GetFileStream メソッドを確認しました

public Stream GetFileStream(文字列ファイル名)
        {
            if (fileEntries == null)
                fileEntries = ParseCentralDirectory(); //Silverligth が好まない形式の zip がある場合に備えて、これを行う必要があります
            ロングポジション = this.stream.Position;
            this.stream.Seek(0, SeekOrigin.Begin);
            URI fileUri = new Uri(filename, UriKind.Relative);
            StreamResourceInfo 情報 = 新しい StreamResourceInfo(this.stream, null);
            StreamResourceInfo ストリーム = System.Windows.Application.GetResourceStream(info, fileUri);
            this.stream.Position = 位置;
            if (ストリーム != null)
                stream.Stream を返します。
            null を返します。
        }

この行で OutOfMemory 例外をスローします。

StreamResourceInfo ストリーム = System.Windows.Application.GetResourceStream(info, fileUri);
4

0 に答える 0