diff --git a/Files.Package/Files.Package.wapproj b/Files.Package/Files.Package.wapproj index 7b6a2629c8f0..7cfaf456b67e 100644 --- a/Files.Package/Files.Package.wapproj +++ b/Files.Package/Files.Package.wapproj @@ -477,5 +477,9 @@ + + + + \ No newline at end of file diff --git a/Files.Package/nupkgs/SharpZipLib.1.3.4.nupkg b/Files.Package/nupkgs/SharpZipLib.1.3.4.nupkg new file mode 100644 index 000000000000..ef5a82d42d59 Binary files /dev/null and b/Files.Package/nupkgs/SharpZipLib.1.3.4.nupkg differ diff --git a/Files.Launcher/nupkgs/microsoft.management.infrastructure.runtime.win.2.0.1.nupkg b/Files.Package/nupkgs/microsoft.management.infrastructure.runtime.win.2.0.1.nupkg similarity index 100% rename from Files.Launcher/nupkgs/microsoft.management.infrastructure.runtime.win.2.0.1.nupkg rename to Files.Package/nupkgs/microsoft.management.infrastructure.runtime.win.2.0.1.nupkg diff --git a/Files/Files.csproj b/Files/Files.csproj index 5cfcae6ab286..6d9e6033b3cf 100644 --- a/Files/Files.csproj +++ b/Files/Files.csproj @@ -1466,9 +1466,6 @@ 2.7.0 - - 5.0.0 - 2.0.1 @@ -1476,7 +1473,7 @@ 13.0.1 - 1.3.3 + 1.3.4 2.0.7 diff --git a/Files/Filesystem/StorageItems/FtpStorageFile.cs b/Files/Filesystem/StorageItems/FtpStorageFile.cs index b5dcc97dffd4..5f315284ab4b 100644 --- a/Files/Filesystem/StorageItems/FtpStorageFile.cs +++ b/Files/Filesystem/StorageItems/FtpStorageFile.cs @@ -178,31 +178,21 @@ public override IAsyncOperation OpenAsync(FileAccessMode ac if (accessMode == FileAccessMode.Read) { var inStream = await ftpClient.OpenReadAsync(FtpPath, cancellationToken); - return new NonSeekableRandomAccessStream(inStream, (ulong)inStream.Length) + return new NonSeekableRandomAccessStreamForRead(inStream, (ulong)inStream.Length) { DisposeCallback = () => ftpClient.Dispose() }; } else { - return new RandomAccessStreamWithFlushCallback() + return new NonSeekableRandomAccessStreamForWrite(await ftpClient.OpenWriteAsync(FtpPath, cancellationToken)) { - DisposeCallback = () => ftpClient.Dispose(), - FlushCallback = UploadFile(ftpClient) + DisposeCallback = () => ftpClient.Dispose() }; } }); } - private Func> UploadFile(FtpClient ftpClient) - { - return (stream) => AsyncInfo.Run(async (cancellationToken) => - { - await ftpClient.UploadAsync(stream.CloneStream().AsStream(), FtpPath, FtpRemoteExists.Overwrite); - return true; - }); - } - public override IAsyncOperation OpenTransactedWriteAsync() => throw new NotSupportedException(); public override IAsyncOperation CopyAsync(IStorageFolder destinationFolder) @@ -267,7 +257,7 @@ public override IAsyncOperation OpenReadAsyn } var inStream = await ftpClient.OpenReadAsync(FtpPath, cancellationToken); - var nsStream = new NonSeekableRandomAccessStream(inStream, (ulong)inStream.Length) + var nsStream = new NonSeekableRandomAccessStreamForRead(inStream, (ulong)inStream.Length) { DisposeCallback = () => ftpClient.Dispose() }; diff --git a/Files/Filesystem/StorageItems/StreamWithContentType.cs b/Files/Filesystem/StorageItems/StreamWithContentType.cs index 6d71056d76a7..65b47c2b6b31 100644 --- a/Files/Filesystem/StorageItems/StreamWithContentType.cs +++ b/Files/Filesystem/StorageItems/StreamWithContentType.cs @@ -10,74 +10,98 @@ namespace Files.Filesystem.StorageItems { public class InputStreamWithDisposeCallback : IInputStream { - private IInputStream stream; + private Stream stream; + private IInputStream iStream; public Action DisposeCallback { get; set; } public InputStreamWithDisposeCallback(Stream stream) { - this.stream = stream.AsInputStream(); + this.stream = stream; + this.iStream = stream.AsInputStream(); } public IAsyncOperationWithProgress ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) { - return stream.ReadAsync(buffer, count, options); + return iStream.ReadAsync(buffer, count, options); } public void Dispose() { + iStream.Dispose(); stream.Dispose(); DisposeCallback?.Invoke(); } } - public class RandomAccessStreamWithFlushCallback : IRandomAccessStream + public class NonSeekableRandomAccessStreamForWrite : IRandomAccessStream { + private Stream stream; + private IOutputStream oStream; private IRandomAccessStream imrac; + private ulong byteSize; private bool isWritten; - public Func> FlushCallback { get; set; } + public Action DisposeCallback { get; set; } - public RandomAccessStreamWithFlushCallback() + public NonSeekableRandomAccessStreamForWrite(Stream stream) { + this.stream = stream; + this.oStream = stream.AsOutputStream(); this.imrac = new InMemoryRandomAccessStream(); } public IInputStream GetInputStreamAt(ulong position) { - return imrac.GetInputStreamAt(position); + throw new NotSupportedException(); } public IOutputStream GetOutputStreamAt(ulong position) { - return imrac.GetOutputStreamAt(position); + if (position != 0) + { + throw new NotSupportedException(); + } + return this; } public void Seek(ulong position) { - imrac.Seek(position); + if (position != 0) + { + throw new NotSupportedException(); + } } - public IRandomAccessStream CloneStream() - { - return imrac.CloneStream(); - } + public IRandomAccessStream CloneStream() => throw new NotSupportedException(); - public bool CanRead => imrac.CanRead; + public bool CanRead => false; - public bool CanWrite => imrac.CanWrite; + public bool CanWrite => true; - public ulong Position => imrac.Position; + public ulong Position => byteSize; - public ulong Size { get => imrac.Size; set => imrac.Size = value; } + public ulong Size + { + get => byteSize; + set => throw new NotSupportedException(); + } public IAsyncOperationWithProgress ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) { - return imrac.ReadAsync(buffer, count, options); + throw new NotSupportedException(); } public IAsyncOperationWithProgress WriteAsync(IBuffer buffer) { - return imrac.WriteAsync(buffer); + Func, Task> taskProvider = + async (token, progress) => + { + var res = await oStream.WriteAsync(buffer); + byteSize += res; + return res; + }; + + return AsyncInfo.Run(taskProvider); } public IAsyncOperation FlushAsync() @@ -89,17 +113,23 @@ public IAsyncOperation FlushAsync() isWritten = true; - return FlushCallback(this) ?? imrac.FlushAsync(); + return AsyncInfo.Run(async (cancellationToken) => + { + await stream.FlushAsync(); + return true; + }); } public void Dispose() { + oStream.Dispose(); + stream.Dispose(); imrac.Dispose(); DisposeCallback?.Invoke(); } } - public class NonSeekableRandomAccessStream : IRandomAccessStream + public class NonSeekableRandomAccessStreamForRead : IRandomAccessStream { private Stream stream; private IRandomAccessStream imrac; @@ -109,7 +139,7 @@ public class NonSeekableRandomAccessStream : IRandomAccessStream public Action DisposeCallback { get; set; } - public NonSeekableRandomAccessStream(Stream baseStream, ulong size) + public NonSeekableRandomAccessStreamForRead(Stream baseStream, ulong size) { this.stream = baseStream; this.imrac = new InMemoryRandomAccessStream(); @@ -135,9 +165,9 @@ public void Seek(ulong position) this.virtualPosition = position; } - public IRandomAccessStream CloneStream() => imrac.CloneStream(); + public IRandomAccessStream CloneStream() => throw new NotSupportedException(); - public bool CanRead => imrac.CanRead; + public bool CanRead => true; public bool CanWrite => false; diff --git a/Files/Filesystem/StorageItems/ZipStorageFile.cs b/Files/Filesystem/StorageItems/ZipStorageFile.cs index 60d50c2ffecf..c050b838bb8f 100644 --- a/Files/Filesystem/StorageItems/ZipStorageFile.cs +++ b/Files/Filesystem/StorageItems/ZipStorageFile.cs @@ -56,19 +56,19 @@ public override IAsyncOperation OpenAsync(FileAccessMode ac } } - ZipFile zipFile = await OpenZipFileAsync(accessMode); - if (zipFile == null) - { - return null; - } - zipFile.IsStreamOwner = true; - var znt = new ZipNameTransform(ContainerPath); - var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (!rw) { + ZipFile zipFile = await OpenZipFileAsync(accessMode); + if (zipFile == null) + { + return null; + } + zipFile.IsStreamOwner = true; + var znt = new ZipNameTransform(ContainerPath); + var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { - return new NonSeekableRandomAccessStream(zipFile.GetInputStream(entry), (ulong)entry.Size) + return new NonSeekableRandomAccessStreamForRead(zipFile.GetInputStream(entry), (ulong)entry.Size) { DisposeCallback = () => zipFile.Close() }; @@ -76,11 +76,37 @@ public override IAsyncOperation OpenAsync(FileAccessMode ac } else { - return new RandomAccessStreamWithFlushCallback() + var znt = new ZipNameTransform(ContainerPath); + var zipDesiredName = znt.TransformFile(Path); + + using (ZipFile zipFile = await OpenZipFileAsync(accessMode)) { - DisposeCallback = () => zipFile.Close(), - FlushCallback = WriteZipEntry(zipFile) - }; + var entry = zipFile.GetEntry(zipDesiredName); + if (entry != null) + { + zipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); + zipFile.Delete(entry); + zipFile.CommitUpdate(); + } + } + + if (BackingFile != null) + { + var zos = new ZipOutputStream((await BackingFile.OpenAsync(FileAccessMode.ReadWrite)).AsStream(), true); + await zos.PutNextEntryAsync(new ZipEntry(zipDesiredName)); + return new NonSeekableRandomAccessStreamForWrite(zos); + } + else + { + var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, true); + if (hFile.IsInvalid) + { + return null; + } + var zos = new ZipOutputStream(new FileStream(hFile, FileAccess.ReadWrite), true); + await zos.PutNextEntryAsync(new ZipEntry(zipDesiredName)); + return new NonSeekableRandomAccessStreamForWrite(zos); + } } return null; }); @@ -249,7 +275,7 @@ public override IAsyncOperation OpenReadAsyn var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { - var nsStream = new NonSeekableRandomAccessStream(zipFile.GetInputStream(entry), (ulong)entry.Size) + var nsStream = new NonSeekableRandomAccessStreamForRead(zipFile.GetInputStream(entry), (ulong)entry.Size) { DisposeCallback = () => zipFile.Close() }; @@ -382,7 +408,7 @@ await NativeFileOperationsHelper.OpenProtectedFileForRead(ContainerPath) : { return null; } - return new ZipFile(new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read)); + return new ZipFile((Stream)new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read)); } }); } @@ -428,32 +454,6 @@ private StreamedFileDataRequestedHandler ZipDataStreamingHandler(string name) }; } - private Func> WriteZipEntry(ZipFile zipFile) - { - return (stream) => AsyncInfo.Run((cancellationToken) => Task.Run(() => - { - try - { - var znt = new ZipNameTransform(ContainerPath); - var zipDesiredName = znt.TransformFile(Path); - var entry = zipFile.GetEntry(zipDesiredName); - - zipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); - if (entry != null) - { - zipFile.Delete(entry); - } - zipFile.Add(new StreamDataSource(stream), zipDesiredName); - zipFile.CommitUpdate(); - } - catch (Exception ex) - { - App.Logger.Warn(ex, "Error writing zip file"); - } - return true; - })); - } - private async Task GetBasicProperties() { using (ZipFile zipFile = await OpenZipFileAsync(FileAccessMode.Read)) @@ -510,18 +510,6 @@ public ZipFileBasicProperties(ZipEntry entry) public override ulong Size => (ulong)zipEntry.Size; } - private class StreamDataSource : IStaticDataSource - { - private IRandomAccessStream stream; - - public StreamDataSource(IRandomAccessStream stream) - { - this.stream = stream; - } - - public Stream GetSource() => stream.CloneStream().AsStream(); - } - #endregion } } diff --git a/Files/Filesystem/StorageItems/ZipStorageFolder.cs b/Files/Filesystem/StorageItems/ZipStorageFolder.cs index 224d68e11857..cc854606fc89 100644 --- a/Files/Filesystem/StorageItems/ZipStorageFolder.cs +++ b/Files/Filesystem/StorageItems/ZipStorageFolder.cs @@ -580,7 +580,7 @@ private IAsyncOperation OpenZipFileAsync(FileAccessMode accessMode) { return null; } - return new ZipFile(new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read)); + return new ZipFile((Stream)new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read)); } }); } diff --git a/nuget.config b/nuget.config index ce89c6e63e99..6ea09e8028f4 100644 --- a/nuget.config +++ b/nuget.config @@ -1,7 +1,7 @@ - + - + \ No newline at end of file