Ich habe eine UWP-App (Universal Windows Platform). Ich versuche, Ordner und seinen Inhalt (Unterordner, Bilder, Textdateien) zu komprimieren.Leere Textdatei (Größe 0KB) konnte nicht in UWP (Windows 10 App) gezippt werden. C#
Es funktioniert gut, wenn die Textdatei nicht leer ist.
Aber es gibt eine Ausnahme, wenn eine Textdatei leer oder leer ist (0KB Größe).
Es folgt der Fehler:
{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()}
Dies ist mein Code:
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);
}
es irgendwelche Arbeit um für dieses Problem? Vielen Dank.