コード例 #1
0
        public static async Task CreateEntryFromFileTest()
        {
            //add file
            String testArchive = StreamHelpers.CreateTempCopyFile(zfile("normal.zip"));

            using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update))
            {
                ZipArchiveEntry e = archive.CreateEntryFromFile(zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
                Assert.NotNull(e);
            }

            await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read, true, true);
        }
コード例 #2
0
        public static void InvalidInstanceMethods()
        {
            String zipFileName = StreamHelpers.CreateTempCopyFile(zfile("normal.zip"));

            using (ZipArchive archive = ZipFile.Open(zipFileName, ZipArchiveMode.Update))
            {
                //non-existent entry
                Assert.True(null == archive.GetEntry("nonExistentEntry"));
                //null/empty string
                Assert.Throws <ArgumentNullException>(() => archive.GetEntry(null));

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

                //null/empty string
                Assert.Throws <ArgumentException>(() => archive.CreateEntry(""));
                Assert.Throws <ArgumentNullException>(() => archive.CreateEntry(null));
            }
        }
コード例 #3
0
        private static void UnsupportedCompressionRoutine(String filename, Boolean throwsOnOpen)
        {
            using (ZipArchive archive = ZipFile.OpenRead(filename))
            {
                ZipArchiveEntry e = archive.Entries[0];
                if (throwsOnOpen)
                {
                    Assert.Throws <InvalidDataException>(() => e.Open());
                }
                else
                {
                    using (Stream s = e.Open())
                    {
                        Assert.Throws <InvalidDataException>(() => s.ReadByte());
                    }
                }
            }

            String         updatedCopyName = StreamHelpers.CreateTempCopyFile(filename);
            String         name;
            Int64          length, compressedLength;
            DateTimeOffset lastWriteTime;

            using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update))
            {
                ZipArchiveEntry e = archive.Entries[0];
                name             = e.FullName;
                lastWriteTime    = e.LastWriteTime;
                length           = e.Length;
                compressedLength = e.CompressedLength;
                Assert.Throws <InvalidDataException>(() => e.Open());
            }

            //make sure that update mode preserves that unreadable file
            using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update))
            {
                ZipArchiveEntry e = archive.Entries[0];
                Assert.Equal(name, e.FullName);
                Assert.Equal(lastWriteTime, e.LastWriteTime);
                Assert.Equal(length, e.Length);
                Assert.Equal(compressedLength, e.CompressedLength);
                Assert.Throws <InvalidDataException>(() => e.Open());
            }
        }
コード例 #4
0
        public static async Task UpdateAddFile()
        {
            //add file
            String testArchive = StreamHelpers.CreateTempCopyFile(zfile("normal.zip"));

            using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update))
            {
                await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
            }

            await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read);

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

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

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

            await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read);


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

            using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update))
            {
                await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");

                var x = archive.Entries;
            }

            await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read);
        }