Skip to content

Commit ceb9720

Browse files
committed
Use a temp file
1 parent 90bd285 commit ceb9720

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

LibZipSharp.UnitTest/ZipTests.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,6 @@ public void InMemoryZipFile ()
272272
zip.AddFile (Path.Combine (fileRoot, "object_spawn.json"), "object_spawn.json");
273273
}
274274

275-
stream.Position = 0;
276-
File.WriteAllBytes ("InMemory1.zip", stream.ToArray ());
277-
278275
stream.Position = 0;
279276
using (var zip = ZipArchive.Open (stream)) {
280277
Assert.AreEqual (3, zip.EntryCount);
@@ -290,9 +287,6 @@ public void InMemoryZipFile ()
290287
zip.AddEntry ("info.json", File.ReadAllText (filePath), Encoding.UTF8, CompressionMethod.Deflate);
291288
}
292289

293-
stream.Position = 0;
294-
File.WriteAllBytes ("InMemory2.zip", stream.ToArray ());
295-
296290
stream.Position = 0;
297291
using (var zip = ZipArchive.Open (stream)) {
298292
Assert.AreEqual (3, zip.EntryCount);
@@ -308,9 +302,6 @@ public void InMemoryZipFile ()
308302

309303
}
310304
}
311-
312-
stream.Position = 0;
313-
File.WriteAllBytes ("InMemory3.zip", stream.ToArray ());
314305
}
315306
}
316307

LibZipSharp/Xamarin.Tools.Zip/ZipArchive.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public abstract partial class ZipArchive : IDisposable, IEnumerable <ZipEntry>
4242
{
4343
internal class CallbackContext : IDisposable {
4444
bool disposed;
45-
public Stream Source = null;
46-
public Stream Destination = null;
45+
public Stream Source { get; set; } = null;
46+
public Stream Destination {get; set;} = null;
47+
public string DestinationFileName {get; set; } = null;
4748

4849
protected virtual void Dispose (bool disposing)
4950
{
@@ -57,6 +58,10 @@ protected virtual void Dispose (bool disposing)
5758
Destination.Dispose ();
5859
Destination = null;
5960
}
61+
if (!string.IsNullOrEmpty (DestinationFileName)&& File.Exists (DestinationFileName)) {
62+
File.Delete (DestinationFileName);
63+
DestinationFileName = null;
64+
}
6065
}
6166
disposed = true;
6267
}
@@ -855,7 +860,8 @@ internal static unsafe Int64 stream_callback (IntPtr state, IntPtr data, UInt64
855860
context.Destination = null;
856861
break;
857862
case SourceCommand.RollbackWrite:
858-
// err do something?
863+
destination.Dispose ();
864+
context.Destination = null;
859865
break;
860866

861867
case SourceCommand.Read:
@@ -869,7 +875,14 @@ internal static unsafe Int64 stream_callback (IntPtr state, IntPtr data, UInt64
869875
ArrayPool<byte>.Shared.Return (buffer);
870876
}
871877
case SourceCommand.BeginWrite:
872-
context.Destination = new MemoryStream ();
878+
try {
879+
string tempFile = Path.GetTempFileName ();
880+
context.Destination = File.Open (tempFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
881+
context.DestinationFileName = tempFile;
882+
} catch (IOException) {
883+
// ok use a memory stream as a backup
884+
context.Destination = new MemoryStream ();
885+
}
873886
destination = context.Destination;
874887
destination.Position = 0;
875888
break;
@@ -886,12 +899,6 @@ internal static unsafe Int64 stream_callback (IntPtr state, IntPtr data, UInt64
886899
handle.Free ();
887900
break;
888901

889-
case SourceCommand.Error:
890-
break;
891-
892-
case SourceCommand.Remove:
893-
break;
894-
895902
case SourceCommand.Supports:
896903
var supports = (Int64)Native.zip_source_make_command_bitmap (
897904
SourceCommand.Open,

0 commit comments

Comments
 (0)