2017-04-24 5 views
0

を圧縮できませんでした。私はフォルダとその内容(サブフォルダ、画像、テキストファイル)を圧縮しようとしています。は私がUWPアプリ(ユニバーサルWindowsのプラットフォーム)を持っているUWP(Windowsの10 APP)での空白のテキストファイル(サイズ0キロバイト)C#

テキストファイルが空でない場合はうまく動作します。

しかし、任意のテキストファイルが空白または空(0キロバイトのサイズ)であるときには、例外を提供します。続き

は誤りです:

{System.ArgumentException: The specified buffer index is not within the buffer capacity. at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count) at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source) at SmartflowRuntimes.Service.d__54.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at SmartflowRuntimes.Service.d__54.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at SmartflowRuntimes.Service.d__53.MoveNext()}

これは私のコードです:

public async Task<string> ZipHelper(string zipFileName) 
{ 
    try 
    { 
     string appFolderPath = ApplicationData.Current.LocalFolder.Path; 
     StorageFolder destnFolder = await StorageFolder.GetFolderFromPathAsync(appFolderPath + "\\destn"); 
     StorageFolder sourceFolder = await StorageFolder.GetFolderFromPathAsync(appFolderPath + "\\src"); 

     StorageFile zipFile = await destnFolder.CreateFileAsync(zipFileName, CreationCollisionOption.ReplaceExisting); 
     Stream zipToCreate = await zipFile.OpenStreamForWriteAsync(); 
     ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Update); 

     await ZipFolderContents(sourceFolder, archive, sourceFolder.Path); 
     archive.Dispose(); 
     return "success"; 
    } 
    catch (Exception ex) 
    { 
     return "fail"; 
    } 
} 

public async Task ZipFolderContents(StorageFolder sourceFolder, ZipArchive archive, string baseDirPath) 
{ 
    IReadOnlyList<StorageFile> files = await sourceFolder.GetFilesAsync(); 

    foreach (StorageFile file in files) 
    { 
     ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Path.Remove(0, baseDirPath.Length)); 
     byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(file)); 
     using (Stream entryStream = readmeEntry.Open()) 
     { 
      await entryStream.WriteAsync(buffer, 0, buffer.Length); 
     } 
    } 

    IReadOnlyList<StorageFolder> subFolders = await sourceFolder.GetFoldersAsync(); 

    if (subFolders.Count() == 0) return; 

    foreach (StorageFolder subfolder in subFolders) 
     await ZipFolderContents(subfolder, archive, baseDirPath); 
} 

は、この問題の周りのすべての作業はありますか?ありがとう。

答えて

1

このSystem.ArgumentExceptionはIBufferのサイズが0より大きい

だからあなたはかなり頻繁にチェックすることと同じように、この問題を解決する、このメソッドを呼び出す前に、ファイルサイズを確認するためであることを期待しWindowsRuntimeBufferExtensions.ToArray方法からスローされますそのメソッドを呼び出す前にnullのオブジェクト参照ところで

ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Path.Remove(0, baseDirPath.Length)); 

ulong fileSize = (await file.GetBasicPropertiesAsync()).Size;     
byte[] buffer = fileSize > 0 ? (await FileIO.ReadBufferAsync(file)).ToArray() 
       : new byte[0]; 

using (Stream entryStream = readmeEntry.Open()) 
{ 
    await entryStream.WriteAsync(buffer, 0, buffer.Length);      
} 

は、WindowsRuntimeBufferExtensions.ToArrayはあなたが(その拡張メソッドと呼ばれている)IBufferインスタンスに存在しないToArrayメソッドを呼び出すことが可能にする、extension methodです。より便利な方法で呼び出す方法も示します。

関連する問題