CreateTempCopyStream() public static method

public static CreateTempCopyStream ( string path ) : Task
path string
return Task
        public static async Task InvalidInstanceMethods()
        {
            Stream zipFile = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
            {
                //non-existent entry
                Assert.True(null == archive.GetEntry("nonExistentEntry"));           //"Should return null on non-existent entry name"
                //null/empty string
                Assert.Throws <ArgumentNullException>(() => archive.GetEntry(null)); //"Should throw on null entry name"

                ZipArchiveEntry entry = archive.GetEntry("first.txt");

                //null/empty string
                AssertExtensions.Throws <ArgumentException>("entryName", () => archive.CreateEntry("")); //"Should throw on empty entry name"
                Assert.Throws <ArgumentNullException>(() => archive.CreateEntry(null));                  //"should throw on null entry name"
            }
        }
コード例 #2
0
        private static async Task updateArchive(ZipArchive archive, string installFile, string entryName)
        {
            ZipArchiveEntry e = archive.CreateEntry(entryName);

            var file = FileData.GetFile(installFile);

            e.LastWriteTime = file.LastModifiedDate;
            Assert.Equal(e.LastWriteTime, file.LastModifiedDate);

            using (var stream = await StreamHelpers.CreateTempCopyStream(installFile))
            {
                using (Stream es = e.Open())
                {
                    es.SetLength(0);
                    stream.CopyTo(es);
                }
            }
        }
コード例 #3
0
 public static async Task UpdateReadNormal()
 {
     ZipTest.IsZipSameAsDir(
         await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip")), ZipTest.zfolder("normal"), ZipArchiveMode.Update, false, false);
     ZipTest.IsZipSameAsDir(
         await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("fake64.zip")), ZipTest.zfolder("small"), ZipArchiveMode.Update, false, false);
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         ZipTest.IsZipSameAsDir(
             await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("unicode.zip")), ZipTest.zfolder("unicode"), ZipArchiveMode.Update, false, false);
     }
     ZipTest.IsZipSameAsDir(
         await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("empty.zip")), ZipTest.zfolder("empty"), ZipArchiveMode.Update, false, false);
     ZipTest.IsZipSameAsDir(
         await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("appended.zip")), ZipTest.zfolder("small"), ZipArchiveMode.Update, false, false);
     ZipTest.IsZipSameAsDir(
         await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("prepended.zip")), ZipTest.zfolder("small"), ZipArchiveMode.Update, false, false);
 }
コード例 #4
0
        public static async Task UpdateZipArchive_AppendTo_CorruptedFileEntry()
        {
            MemoryStream stream = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));

            int    updatedUncompressedLength = 1310976;
            string append = "\r\n\r\nThe answer my friend, is blowin' in the wind.";

            byte[] data = Encoding.ASCII.GetBytes(append);
            long   oldCompressedSize = 0;

            int nameOffset = PatchDataRelativeToFileName(Encoding.ASCII.GetBytes(s_tamperedFileName), stream, 8);                         // patch uncompressed size in file header

            PatchDataRelativeToFileName(Encoding.ASCII.GetBytes(s_tamperedFileName), stream, 22, nameOffset + s_tamperedFileName.Length); // patch in central directory too

            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Update, true))
            {
                ZipArchiveEntry e = archive.GetEntry(s_tamperedFileName);
                oldCompressedSize = e.CompressedLength;
                using (Stream s = e.Open())
                {
                    Assert.Equal(updatedUncompressedLength, s.Length);
                    s.Seek(0, SeekOrigin.End);
                    s.Write(data, 0, data.Length);
                    Assert.Equal(updatedUncompressedLength + data.Length, s.Length);
                }
            }

            using (ZipArchive modifiedArchive = new ZipArchive(stream, ZipArchiveMode.Read))
            {
                ZipArchiveEntry e = modifiedArchive.GetEntry(s_tamperedFileName);
                using (Stream s = e.Open())
                    using (var ms = new MemoryStream())
                    {
                        await s.CopyToAsync(ms, s_bufferSize);

                        Assert.Equal(updatedUncompressedLength + data.Length, ms.Length);
                        ms.Seek(updatedUncompressedLength, SeekOrigin.Begin);
                        byte[] read = new byte[data.Length];
                        ms.Read(read, 0, data.Length);
                        Assert.Equal(append, Encoding.ASCII.GetString(read));
                    }
                Assert.True(oldCompressedSize > e.CompressedLength); // old compressed size must be reduced by Uncomressed size limit
            }
        }
コード例 #5
0
        public static async Task UpdateZipArchive_OverwriteCorruptedEntry()
        {
            MemoryStream stream = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));

            int    updatedUncompressedLength = 1310976;
            string overwrite = "\r\n\r\nThe answer my friend, is blowin' in the wind.";

            byte[] data = Encoding.ASCII.GetBytes(overwrite);

            int nameOffset = PatchDataRelativeToFileName(Encoding.ASCII.GetBytes(s_tamperedFileName), stream, 8);                         // patch uncompressed size in file header

            PatchDataRelativeToFileName(Encoding.ASCII.GetBytes(s_tamperedFileName), stream, 22, nameOffset + s_tamperedFileName.Length); // patch in central directory too

            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Update, true))
            {
                ZipArchiveEntry e        = archive.GetEntry(s_tamperedFileName);
                string          fileName = zmodified(Path.Combine("overwrite", "first.txt"));
                var             file     = FileData.GetFile(fileName);

                using (var s = new MemoryStream(data))
                    using (Stream es = e.Open())
                    {
                        Assert.Equal(updatedUncompressedLength, es.Length);
                        es.SetLength(0);
                        await s.CopyToAsync(es, s_bufferSize);

                        Assert.Equal(data.Length, es.Length);
                    }
            }

            using (ZipArchive modifiedArchive = new ZipArchive(stream, ZipArchiveMode.Read))
            {
                ZipArchiveEntry e = modifiedArchive.GetEntry(s_tamperedFileName);
                using (Stream s = e.Open())
                    using (var ms = new MemoryStream())
                    {
                        await s.CopyToAsync(ms, s_bufferSize);

                        Assert.Equal(data.Length, ms.Length);
                        Assert.Equal(overwrite, Encoding.ASCII.GetString(ms.GetBuffer(), 0, data.Length));
                    }
            }
        }
コード例 #6
0
        public static async Task ZipArchive_InvalidEntry(string zipname, bool throwsOnOpen)
        {
            string filename = bad(zipname);

            using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(filename), ZipArchiveMode.Read))
            {
                ZipArchiveEntry e = archive.Entries[0];
                if (throwsOnOpen)
                {
                    Assert.Throws <InvalidDataException>(() => e.Open()); //"should throw on open"
                }
                else
                {
                    using (Stream s = e.Open())
                    {
                        Assert.Throws <InvalidDataException>(() => s.ReadByte()); //"Unreadable stream"
                    }
                }
            }
        }
コード例 #7
0
 public static async Task RoundTrips_UnixFilePermissions(int expectedAttr)
 {
     using (var stream = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip")))
     {
         using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Update, true))
         {
             foreach (ZipArchiveEntry e in archive.Entries)
             {
                 e.ExternalAttributes = expectedAttr;
                 Assert.Equal(expectedAttr, e.ExternalAttributes);
             }
         }
         using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
         {
             foreach (ZipArchiveEntry e in archive.Entries)
             {
                 Assert.Equal(expectedAttr, e.ExternalAttributes);
             }
         }
     }
 }
コード例 #8
0
        public static async Task CreateFromDir(String directory, Stream archiveStream, ZipArchiveMode mode)
        {
            var files = FileData.InPath(directory);

            using (ZipArchive archive = new ZipArchive(archiveStream, mode, true))
            {
                foreach (var i in files)
                {
                    if (i.IsFolder)
                    {
                        String entryName = i.FullName;

                        archive.CreateEntry(entryName.Replace('\\', '/') + "/");
                    }
                }

                foreach (var i in files)
                {
                    if (i.IsFile)
                    {
                        String entryName = i.FullName;

                        var installStream = await StreamHelpers.CreateTempCopyStream(Path.Combine(i.OrigFolder, i.FullName));

                        if (installStream != null)
                        {
                            ZipArchiveEntry e = archive.CreateEntry(entryName.Replace('\\', '/'));
                            try
                            { e.LastWriteTime = i.LastModifiedDate; }
                            catch (ArgumentOutOfRangeException)
                            { e.LastWriteTime = DateTimeOffset.Now; }
                            using (Stream entryStream = e.Open())
                            {
                                installStream.CopyTo(entryStream);
                            }
                        }
                    }
                }
            }
        }
コード例 #9
0
        public static async Task DeleteAndMoveEntries()
        {
            //delete and move
            var testArchive = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                ZipArchiveEntry toBeDeleted = archive.GetEntry("binary.wmv");
                toBeDeleted.Delete();
                toBeDeleted.Delete(); //delete twice should be okay
                ZipArchiveEntry moved = archive.CreateEntry("notempty/secondnewname.txt");
                ZipArchiveEntry orig  = archive.GetEntry("notempty/second.txt");
                using (Stream origMoved = orig.Open(), movedStream = moved.Open())
                {
                    origMoved.CopyTo(movedStream);
                }
                moved.LastWriteTime = orig.LastWriteTime;
                orig.Delete();
            }

            IsZipSameAsDir(testArchive, zmodified("deleteMove"), ZipArchiveMode.Read, requireExplicit: true, checkTimes: true);
        }
コード例 #10
0
ファイル: zip_ReadTests.cs プロジェクト: mikem8361/runtime
        public static async Task IdentifyEncryptedEntries(string zipFile)
        {
            var entriesEncrypted = new Dictionary <string, bool>();

            using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(zfile(zipFile)), ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    entriesEncrypted.Add(entry.Name, entry.IsEncrypted);
                }
            }

            var expectedEntries = new Dictionary <string, bool>()
            {
                { "file1-encrypted.txt", true },
                { "file2-unencrypted.txt", false },
                { "file3-encrypted.txt", true },
                { "file4-unencrypted.txt", false },
            };

            Assert.Equal(expectedEntries, entriesEncrypted);
        }
コード例 #11
0
ファイル: zip_UpdateTests.cs プロジェクト: jnm2/corefx
        public static async Task AppendToEntry()
        {
            //append
            Stream testArchive = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                ZipArchiveEntry e = archive.GetEntry("first.txt");

                using (StreamWriter s = new StreamWriter(e.Open()))
                {
                    s.BaseStream.Seek(0, SeekOrigin.End);

                    s.Write("\r\n\r\nThe answer my friend, is blowin' in the wind.");
                }

                var file = FileData.GetFile(zmodified(Path.Combine("append", "first.txt")));
                e.LastWriteTime = file.LastModifiedDate;
            }

            IsZipSameAsDir(testArchive, zmodified("append"), ZipArchiveMode.Read, requireExplicit: true, checkTimes: true);
        }
コード例 #12
0
ファイル: zip_ReadTests.cs プロジェクト: wendellcmd/corefx
        public static async Task ReadModeInvalidOpsTest()
        {
            ZipArchive      archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(zfile("normal.zip")), ZipArchiveMode.Read);
            ZipArchiveEntry e       = archive.GetEntry("first.txt");

            //should also do it on deflated stream

            //on archive
            Assert.Throws <NotSupportedException>(() => archive.CreateEntry("hi there")); //"Should not be able to create entry"

            //on entry
            Assert.Throws <NotSupportedException>(() => e.Delete());                             //"Should not be able to delete entry"
            //Throws<NotSupportedException>(() => e.MoveTo("dirka"));
            Assert.Throws <NotSupportedException>(() => e.LastWriteTime = new DateTimeOffset()); //"Should not be able to update time"

            //on stream
            Stream s = e.Open();

            Assert.Throws <NotSupportedException>(() => s.Flush());                   //"Should not be able to flush on read stream"
            Assert.Throws <NotSupportedException>(() => s.WriteByte(25));             //"should not be able to write to read stream"
            Assert.Throws <NotSupportedException>(() => s.Position = 4);              //"should not be able to seek on read stream"
            Assert.Throws <NotSupportedException>(() => s.Seek(0, SeekOrigin.Begin)); //"should not be able to seek on read stream"
            Assert.Throws <NotSupportedException>(() => s.SetLength(0));              //"should not be able to resize read stream"

            archive.Dispose();

            //after disposed
            Assert.Throws <ObjectDisposedException>(() => { var x = archive.Entries; });                //"Should not be able to get entries on disposed archive"
            Assert.Throws <NotSupportedException>(() => archive.CreateEntry("dirka"));                  //"should not be able to create on disposed archive"

            Assert.Throws <ObjectDisposedException>(() => e.Open());                                    //"should not be able to open on disposed archive"
            Assert.Throws <NotSupportedException>(() => e.Delete());                                    //"should not be able to delete on disposed archive"
            Assert.Throws <ObjectDisposedException>(() => { e.LastWriteTime = new DateTimeOffset(); }); //"Should not be able to update on disposed archive"

            Assert.Throws <NotSupportedException>(() => s.ReadByte());                                  //"should not be able to read on disposed archive"

            s.Dispose();
        }
コード例 #13
0
        public static async Task UpdateAddFile()
        {
            //add file
            var testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                await updateArchive(archive, ZipTest.zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
            }

            ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("addFile"), ZipArchiveMode.Read, false, false);

            //add file and read entries before
            testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                var x = archive.Entries;

                await updateArchive(archive, ZipTest.zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
            }

            ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("addFile"), ZipArchiveMode.Read, false, false);


            //add file and read entries after
            testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                await updateArchive(archive, ZipTest.zmodified(Path.Combine("addFile", "added.txt")), "added.txt");

                var x = archive.Entries;
            }

            ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("addFile"), ZipArchiveMode.Read, false, false);
        }
コード例 #14
0
        /// <param name="useSpansForWriting">Tests the Span overloads of Write</param>
        /// <param name="writeInChunks">Writes in chunks of 5 to test Write with a nonzero offset</param>
        public static async Task CreateFromDir(string directory, Stream archiveStream, ZipArchiveMode mode, bool useSpansForWriting = false, bool writeInChunks = false)
        {
            var files = FileData.InPath(directory);

            using (ZipArchive archive = new ZipArchive(archiveStream, mode, true))
            {
                foreach (var i in files)
                {
                    if (i.IsFolder)
                    {
                        string entryName = i.FullName;

                        ZipArchiveEntry e = archive.CreateEntry(entryName.Replace('\\', '/') + "/");
                        e.LastWriteTime = i.LastModifiedDate;
                    }
                }

                foreach (var i in files)
                {
                    if (i.IsFile)
                    {
                        string entryName = i.FullName;

                        var installStream = await StreamHelpers.CreateTempCopyStream(Path.Combine(i.OrigFolder, i.FullName));

                        if (installStream != null)
                        {
                            ZipArchiveEntry e = archive.CreateEntry(entryName.Replace('\\', '/'));
                            e.LastWriteTime = i.LastModifiedDate;
                            using (Stream entryStream = e.Open())
                            {
                                int bytesRead;
                                var buffer = new byte[1024];
                                if (useSpansForWriting)
                                {
                                    while ((bytesRead = installStream.Read(new Span <byte>(buffer))) != 0)
                                    {
                                        entryStream.Write(new ReadOnlySpan <byte>(buffer, 0, bytesRead));
                                    }
                                }
                                else if (writeInChunks)
                                {
                                    while ((bytesRead = installStream.Read(buffer, 0, buffer.Length)) != 0)
                                    {
                                        for (int k = 0; k < bytesRead; k += 5)
                                        {
                                            entryStream.Write(buffer, k, Math.Min(5, bytesRead - k));
                                        }
                                    }
                                }
                                else
                                {
                                    while ((bytesRead = installStream.Read(buffer, 0, buffer.Length)) != 0)
                                    {
                                        entryStream.Write(buffer, 0, bytesRead);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #15
0
        public static async Task IsZipSameAsDirAsync(string archiveFile, string directory, ZipArchiveMode mode, bool requireExplicit, bool checkTimes)
        {
            var s = await StreamHelpers.CreateTempCopyStream(archiveFile);

            IsZipSameAsDir(s, directory, mode, requireExplicit, checkTimes);
        }
コード例 #16
0
 public static async Task UpdateReadNormal(string zipFile, string zipFolder)
 {
     IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(zfile(zipFile)), zfolder(zipFolder), ZipArchiveMode.Update, requireExplicit: true, checkTimes: true);
 }
コード例 #17
0
        public static void ReadArchive_WithEOCDComment_TrailingPrecedingGarbage()
        {
            void InsertEntry(ZipArchive archive, string name, string contents)
            {
                ZipArchiveEntry entry = archive.CreateEntry(name);

                using (StreamWriter writer = new StreamWriter(entry.Open()))
                {
                    writer.WriteLine(contents);
                }
            }

            int GetEntryContentsLength(ZipArchiveEntry entry)
            {
                int length = 0;

                using (Stream stream = entry.Open())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        length = reader.ReadToEnd().Length;
                    }
                }
                return(length);
            }

            void VerifyValidEntry(ZipArchiveEntry entry, string expectedName, int expectedMinLength)
            {
                Assert.NotNull(entry);
                Assert.Equal(expectedName, entry.Name);
                // The file has a few more bytes, but should be at least as large as its contents
                Assert.True(GetEntryContentsLength(entry) >= expectedMinLength);
            }

            string name0   = "huge0.txt";
            string name1   = "huge1.txt";
            string str64KB = new string('x', ushort.MaxValue);

            byte[] byte64KB = Text.Encoding.ASCII.GetBytes(str64KB);

            // Open empty file with 64KB EOCD comment
            string path = strange("extradata/emptyWith64KBComment.zip");

            using (MemoryStream archiveStream = StreamHelpers.CreateTempCopyStream(path).Result)
            {
                // Insert 2 64KB txt entries
                using (ZipArchive archive = new ZipArchive(archiveStream, ZipArchiveMode.Update, leaveOpen: true))
                {
                    InsertEntry(archive, name0, str64KB);
                    InsertEntry(archive, name1, str64KB);
                }

                // Open and verify items
                archiveStream.Seek(0, SeekOrigin.Begin);
                using (ZipArchive archive = new ZipArchive(archiveStream, ZipArchiveMode.Read, leaveOpen: true))
                {
                    Assert.Equal(2, archive.Entries.Count);
                    VerifyValidEntry(archive.Entries[0], name0, ushort.MaxValue);
                    VerifyValidEntry(archive.Entries[1], name1, ushort.MaxValue);
                }

                // Append 64KB of garbage
                archiveStream.Seek(0, SeekOrigin.End);
                archiveStream.Write(byte64KB, 0, byte64KB.Length);

                // Open should not be possible because we can't find the EOCD in the max search length from the end
                Assert.Throws <InvalidDataException>(() =>
                {
                    ZipArchive archive = new ZipArchive(archiveStream, ZipArchiveMode.Read, leaveOpen: true);
                });

                // Create stream with 64KB of prepended garbage, then the above stream appended
                // Attempting to create a ZipArchive should fail: no EOCD found
                using (MemoryStream prependStream = new MemoryStream())
                {
                    prependStream.Write(byte64KB, 0, byte64KB.Length);
                    archiveStream.WriteTo(prependStream);

                    Assert.Throws <InvalidDataException>(() =>
                    {
                        ZipArchive archive = new ZipArchive(prependStream, ZipArchiveMode.Read);
                    });
                }
            }
        }
コード例 #18
0
 public static async Task StrangeFiles4()
 {
     ZipTest.IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(
                                ZipTest.strange("dataDescriptor.zip")), ZipTest.zfolder("normalWithoutBinary"), ZipArchiveMode.Update, true, false);
 }
コード例 #19
0
 public static async Task CompatibilityTestsMsFiles(string withTrailing, string withoutTrailing, bool requireExplicit, bool checkTimes)
 {
     IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(compat(withTrailing)), compat(withoutTrailing), ZipArchiveMode.Update, requireExplicit, checkTimes);
 }
コード例 #20
0
 public static async Task Deflate64Zip()
 {
     IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(compat("deflate64.zip")), zfolder("normal"), ZipArchiveMode.Update, requireExplicit: true, checkTimes: true);
 }
コード例 #21
0
 public static async Task CompatibilityTests(string zipFile, string zipFolder, bool requireExplicit, bool checkTimes)
 {
     IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(compat(zipFile)), zfolder(zipFolder), ZipArchiveMode.Update, requireExplicit, checkTimes);
 }
コード例 #22
0
        public static async Task UpdateModifications()
        {
            //delete and move
            var testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                ZipArchiveEntry toBeDeleted = archive.GetEntry("binary.wmv");
                toBeDeleted.Delete();
                toBeDeleted.Delete(); //delete twice should be okay
                ZipArchiveEntry moved = archive.CreateEntry("notempty/secondnewname.txt");
                ZipArchiveEntry orig  = archive.GetEntry("notempty/second.txt");
                using (Stream origMoved = orig.Open(), movedStream = moved.Open())
                {
                    origMoved.CopyTo(movedStream);
                }
                moved.LastWriteTime = orig.LastWriteTime;
                orig.Delete();
            }

            ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("deleteMove"), ZipArchiveMode.Read, false, false);

            //append
            testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                ZipArchiveEntry e = archive.GetEntry("first.txt");

                using (StreamWriter s = new StreamWriter(e.Open()))
                {
                    s.BaseStream.Seek(0, SeekOrigin.End);

                    s.Write("\r\n\r\nThe answer my friend, is blowin' in the wind.");
                }

                e.LastWriteTime = new DateTimeOffset(2010, 7, 7, 11, 57, 18, new TimeSpan(-7, 0, 0));
            }

            ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("append"), ZipArchiveMode.Read, false, false);

            //Overwrite file
            testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));

            using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
            {
                String          fileName = ZipTest.zmodified(Path.Combine("overwrite", "first.txt"));
                ZipArchiveEntry e        = archive.GetEntry("first.txt");

                var file = FileData.GetFile(fileName);
                e.LastWriteTime = file.LastModifiedDate;

                using (var stream = await StreamHelpers.CreateTempCopyStream(fileName))
                {
                    using (Stream es = e.Open())
                    {
                        es.SetLength(0);
                        stream.CopyTo(es);
                    }
                }
            }

            ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("overwrite"), ZipArchiveMode.Read, false, false);
        }
コード例 #23
0
 public static async Task StrangeFiles1()
 {
     ZipTest.IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(
                                ZipTest.strange(Path.Combine("extradata", "extraDataLHandCDentryAndArchiveComments.zip"))), ZipTest.zfolder("verysmall"), ZipArchiveMode.Update, false, false);
 }
コード例 #24
0
 public static async Task StrangeFiles3()
 {
     ZipTest.IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(
                                ZipTest.strange(Path.Combine("extradata", "zip64ThenExtraData.zip"))), ZipTest.zfolder("verysmall"), ZipArchiveMode.Update, false, false);
 }
コード例 #25
0
 public static async Task UpdateReadNormal(string zipFile, string zipFolder)
 {
     ZipTest.IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(ZipTest.zfile(zipFile)), ZipTest.zfolder(zipFolder), ZipArchiveMode.Update, false, false);
 }
コード例 #26
0
 public static async Task StrangeFiles5()
 {
     ZipTest.IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(
                                ZipTest.strange("filenameTimeAndSizesDifferentInLH.zip")), ZipTest.zfolder("verysmall"), ZipArchiveMode.Update, true, false);
 }
コード例 #27
0
 public static async Task StrangeFiles(string zipFile, string zipFolder, bool requireExplicit)
 {
     IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(strange(zipFile)), zfolder(zipFolder), ZipArchiveMode.Update, requireExplicit, checkTimes: true);
 }
コード例 #28
0
ファイル: zip_ReadTests.cs プロジェクト: wendellcmd/corefx
        public static async Task ReadInterleaved()
        {
            using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"))))
            {
                ZipArchiveEntry e1 = archive.GetEntry("first.txt");
                ZipArchiveEntry e2 = archive.GetEntry("notempty/second.txt");

                //read all of e1 and e2's contents
                Byte[] e1readnormal  = new Byte[e1.Length];
                Byte[] e2readnormal  = new Byte[e2.Length];
                Byte[] e1interleaved = new Byte[e1.Length];
                Byte[] e2interleaved = new Byte[e2.Length];

                using (Stream e1s = e1.Open())
                {
                    ReadBytes(e1s, e1readnormal, e1.Length);
                }
                using (Stream e2s = e2.Open())
                {
                    ReadBytes(e2s, e2readnormal, e2.Length);
                }

                //now read interleaved, assume we are working with < 4gb files
                const Int32 bytesAtATime = 15;

                using (Stream e1s = e1.Open(), e2s = e2.Open())
                {
                    Int32 e1pos = 0;
                    Int32 e2pos = 0;

                    while (e1pos < e1.Length || e2pos < e2.Length)
                    {
                        if (e1pos < e1.Length)
                        {
                            Int32 e1bytesRead = e1s.Read(e1interleaved, e1pos,
                                                         bytesAtATime + e1pos > e1.Length ? (Int32)e1.Length - e1pos : bytesAtATime);
                            e1pos += e1bytesRead;
                        }

                        if (e2pos < e2.Length)
                        {
                            Int32 e2bytesRead = e2s.Read(e2interleaved, e2pos,
                                                         bytesAtATime + e2pos > e2.Length ? (Int32)e2.Length - e2pos : bytesAtATime);
                            e2pos += e2bytesRead;
                        }
                    }
                }

                //now compare to original read
                ArraysEqual <Byte>(e1readnormal, e1interleaved, e1readnormal.Length);
                ArraysEqual <Byte>(e2readnormal, e2interleaved, e2readnormal.Length);

                //now read one entry interleaved
                Byte[] e1selfInterleaved1 = new Byte[e1.Length];
                Byte[] e1selfInterleaved2 = new Byte[e2.Length];


                using (Stream s1 = e1.Open(), s2 = e1.Open())
                {
                    Int32 s1pos = 0;
                    Int32 s2pos = 0;

                    while (s1pos < e1.Length || s2pos < e1.Length)
                    {
                        if (s1pos < e1.Length)
                        {
                            Int32 s1bytesRead = s1.Read(e1interleaved, s1pos,
                                                        bytesAtATime + s1pos > e1.Length ? (Int32)e1.Length - s1pos : bytesAtATime);
                            s1pos += s1bytesRead;
                        }

                        if (s2pos < e1.Length)
                        {
                            Int32 s2bytesRead = s2.Read(e2interleaved, s2pos,
                                                        bytesAtATime + s2pos > e1.Length ? (Int32)e1.Length - s2pos : bytesAtATime);
                            s2pos += s2bytesRead;
                        }
                    }
                }

                //now compare to original read
                ArraysEqual <Byte>(e1readnormal, e1selfInterleaved1, e1readnormal.Length);
                ArraysEqual <Byte>(e1readnormal, e1selfInterleaved2, e1readnormal.Length);
            }
        }
コード例 #29
0
 public static async Task StrangeFiles(string zipFile, string zipFolder, bool dontRequireExplicit)
 {
     ZipTest.IsZipSameAsDir(await StreamHelpers.CreateTempCopyStream(ZipTest.strange(zipFile)), ZipTest.zfolder(zipFolder), ZipArchiveMode.Update, dontRequireExplicit, dontCheckTimes: false);
 }