Answered by:
Zipping some files ends in crash

Question
-
I'm trying to the following to zip some files, but it always ends in a crash with this exception:
A first chance exception of type 'System.ArgumentException' occurred in System.Runtime.WindowsRuntime.NI.DLL
Additional information: The specified buffer index is not within the buffer capacity.
The code:
Private Async Sub ZipFiles() Dim filesToCompress As IReadOnlyList(Of Windows.Storage.StorageFile) = Await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync Using zipMemoryStream As New MemoryStream Using zipArchive As Compression.ZipArchive = New Compression.ZipArchive(zipMemoryStream, Compression.ZipArchiveMode.Create)
For Each fileToCompress As Windows.Storage.StorageFile In filesToCompress ' The following line is the one that's raising the exception
Dim buffer As Byte() = WindowsRuntimeBufferExtensions.ToArray(Await Windows.Storage.FileIO.ReadBufferAsync(fileToCompress)) Dim entry As Compression.ZipArchiveEntry = zipArchive.CreateEntry(fileToCompress.Name) Using entryStream As Stream = entry.Open Await entryStream.WriteAsync(buffer, 0, buffer.Length) End Using Next End Using Dim zipFile As Windows.Storage.StorageFile = Await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("MyZipfile.zip", Windows.Storage.CreationCollisionOption.ReplaceExisting) Using zipStream As Windows.Storage.Streams.IRandomAccessStream = Await zipFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite) Using outstream As Stream = zipStream.AsStreamForWrite Dim buffer() As Byte = zipMemoryStream.ToArray outstream.Write(buffer, 0, buffer.Length) outstream.Flush() End Using End Using End Using End Sub
Any ideas on how make this Work?
Wednesday, January 14, 2015 10:33 PM
Answers
-
Solved. The Exception was raised due to zero-length files.
- Marked as answer by FacilisDK Thursday, January 15, 2015 10:28 PM
Thursday, January 15, 2015 10:28 PM
All replies
-
Hi FacilisDK,
There is not enough information for me to troubleshooting your problem. So please refer to a working sample from MSDN code gallery https://code.msdn.microsoft.com/windowsapps/How-to-and-extract-zip-a4794b24 to see if it is working in your scenario.
If not, please post a repro project here. Use your OneDrive and share a link.
Regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.
Thursday, January 15, 2015 6:30 AMModerator -
Solved. The Exception was raised due to zero-length files.
- Marked as answer by FacilisDK Thursday, January 15, 2015 10:28 PM
Thursday, January 15, 2015 10:28 PM