Пример #1
0
        public void BasicEncryption()
        {
            const string TestValue = "0001000";
            var          memStream = new MemoryStream();

            using (ZipFile f = new ZipFile(memStream))
            {
                f.IsStreamOwner = false;
                f.Password      = "******";

                var m = new StringMemoryDataSource(TestValue);
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
            }

            using (ZipFile g = new ZipFile(memStream))
            {
                g.Password = "******";
                ZipEntry ze = g[0];

                Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted");
                using (StreamReader r = new StreamReader(g.GetInputStream(0)))
                {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }
            }
        }
Пример #2
0
        public void Zip64Useage()
        {
            var memStream = new MemoryStream();

            using (ZipFile f = new ZipFile(memStream))
            {
                f.IsStreamOwner = false;
                f.UseZip64      = UseZip64.On;

                var m = new StringMemoryDataSource("0000000");
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.Add(m, "b.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true));
            }

            byte[] rawArchive = memStream.ToArray();

            byte[] pseudoSfx = new byte[1049 + rawArchive.Length];
            Array.Copy(rawArchive, 0, pseudoSfx, 1049, rawArchive.Length);

            memStream = new MemoryStream(pseudoSfx);
            using (ZipFile f = new ZipFile(memStream))
            {
                for (int index = 0; index < f.Count; ++index)
                {
                    Stream entryStream = f.GetInputStream(index);
                    var    data        = new MemoryStream();
                    StreamUtils.Copy(entryStream, data, new byte[128]);
                    string contents = Encoding.ASCII.GetString(data.ToArray());
                    Assert.AreEqual("0000000", contents);
                }
            }
        }
Пример #3
0
        public void AddEncryptedEntriesToExistingArchive()
        {
            const string TestValue = "0001000";
            var          memStream = new MemoryStream();

            using (ZipFile f = new ZipFile(memStream))
            {
                f.IsStreamOwner = false;
                f.UseZip64      = UseZip64.Off;

                var m = new StringMemoryDataSource(TestValue);
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
            }

            using (ZipFile g = new ZipFile(memStream))
            {
                ZipEntry ze = g[0];

                Assert.IsFalse(ze.IsCrypted, "Entry should NOT be encrypted");
                using (StreamReader r = new StreamReader(g.GetInputStream(0)))
                {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }

                var n = new StringMemoryDataSource(TestValue);

                g.Password      = "******";
                g.UseZip64      = UseZip64.Off;
                g.IsStreamOwner = false;
                g.BeginUpdate();
                g.Add(n, "a1.dat");
                g.CommitUpdate();
                Assert.IsTrue(g.TestArchive(true), "Archive test should pass");
                ze = g[1];
                Assert.IsTrue(ze.IsCrypted, "New entry should be encrypted");

                using (StreamReader r = new StreamReader(g.GetInputStream(0)))
                {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }
            }
        }
Пример #4
0
        public void Zip64Update()
        {
            using (var memStream = new MemoryStream())
            {
                using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
                {
                    f.UseZip64 = UseZip64.On;

                    var m = new StringMemoryDataSource("0000000");
                    f.BeginUpdate(new MemoryArchiveStorage());
                    f.Add(m, "a.dat");
                    f.Add(m, "b.dat");
                    f.CommitUpdate();
                    Assert.That(f.TestArchive(true), Is.True, "initial archive should be valid");
                }

                memStream.Seek(0, SeekOrigin.Begin);

                using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
                {
                    Assert.That(f.Count, Is.EqualTo(2), "Archive should have 2 entries");

                    f.BeginUpdate(new MemoryArchiveStorage());
                    f.Delete("b.dat");
                    f.CommitUpdate();
                    Assert.That(f.TestArchive(true), Is.True, "modified archive should be valid");
                }

                memStream.Seek(0, SeekOrigin.Begin);

                using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
                {
                    Assert.That(f.Count, Is.EqualTo(1), "Archive should have 1 entry");

                    for (int index = 0; index < f.Count; ++index)
                    {
                        Stream entryStream = f.GetInputStream(index);
                        var    data        = new MemoryStream();
                        StreamUtils.Copy(entryStream, data, new byte[128]);
                        string contents = Encoding.ASCII.GetString(data.ToArray());
                        Assert.That(contents, Is.EqualTo("0000000"), "archive member data should be correct");
                    }
                }
            }
        }
Пример #5
0
        public void BasicEncryptionToDisk()
        {
            const string TestValue = "0001000";
            string       tempFile  = GetTempFilePath();

            Assert.IsNotNull(tempFile, "No permission to execute this test?");

            tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");

            using (ZipFile f = ZipFile.Create(tempFile))
            {
                f.Password = "******";

                var m = new StringMemoryDataSource(TestValue);
                f.BeginUpdate();
                f.Add(m, "a.dat");
                f.CommitUpdate();
            }

            using (ZipFile f = new ZipFile(tempFile))
            {
                f.Password = "******";
                Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
            }

            using (ZipFile g = new ZipFile(tempFile))
            {
                g.Password = "******";
                ZipEntry ze = g[0];

                Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted");
                using (StreamReader r = new StreamReader(g.GetInputStream(0)))
                {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }
            }

            File.Delete(tempFile);
        }
Пример #6
0
        public void Zip64Useage()
        {
            MemoryStream memStream = new MemoryStream();
            using (ZipFile f = new ZipFile(memStream)) {
                f.IsStreamOwner = false;
                f.UseZip64 = UseZip64.On;

                StringMemoryDataSource m = new StringMemoryDataSource("0000000");
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.Add(m, "b.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true));
            }

            byte[] rawArchive = memStream.ToArray();

            byte[] pseudoSfx = new byte[1049 + rawArchive.Length];
            Array.Copy(rawArchive, 0, pseudoSfx, 1049, rawArchive.Length);

            memStream = new MemoryStream(pseudoSfx);
            using (ZipFile f = new ZipFile(memStream)) {
                for (int index = 0; index < f.Count; ++index) {
                    Stream entryStream = f.GetInputStream(index);
                    MemoryStream data = new MemoryStream();
                    StreamUtils.Copy(entryStream, data, new byte[128]);
                    string contents = Encoding.ASCII.GetString(data.ToArray());
                    Assert.AreEqual("0000000", contents);
                }
            }
        }
Пример #7
0
        public void BasicEncryptionToDisk()
        {
            const string TestValue = "0001000";
            string tempFile = GetTempFilePath();
            Assert.IsNotNull(tempFile, "No permission to execute this test?");

            tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");

            using (ZipFile f = ZipFile.Create(tempFile)) {
                f.Password = "******";

                StringMemoryDataSource m = new StringMemoryDataSource(TestValue);
                f.BeginUpdate();
                f.Add(m, "a.dat");
                f.CommitUpdate();
            }

            using (ZipFile f = new ZipFile(tempFile)) {
                f.Password = "******";
                Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
            }

            using (ZipFile g = new ZipFile(tempFile)) {
                g.Password = "******";
                ZipEntry ze = g[0];

                Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted");
                using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }
            }

            File.Delete(tempFile);
        }
Пример #8
0
        public void BasicEncryption()
        {
            const string TestValue = "0001000";
            MemoryStream memStream = new MemoryStream();
            using (ZipFile f = new ZipFile(memStream)) {
                f.IsStreamOwner = false;
                f.Password = "******";

                StringMemoryDataSource m = new StringMemoryDataSource(TestValue);
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
            }

            using (ZipFile g = new ZipFile(memStream)) {
                g.Password = "******";
                ZipEntry ze = g[0];

                Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted");
                using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }
            }
        }
Пример #9
0
        public void AddEncryptedEntriesToExistingArchive()
        {
            const string TestValue = "0001000";
            MemoryStream memStream = new MemoryStream();
            using (ZipFile f = new ZipFile(memStream)) {
                f.IsStreamOwner = false;
                f.UseZip64 = UseZip64.Off;

                StringMemoryDataSource m = new StringMemoryDataSource(TestValue);
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
            }

            using (ZipFile g = new ZipFile(memStream)) {
                ZipEntry ze = g[0];

                Assert.IsFalse(ze.IsCrypted, "Entry should NOT be encrypted");
                using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }

                StringMemoryDataSource n = new StringMemoryDataSource(TestValue);

                g.Password = "******";
                g.UseZip64 = UseZip64.Off;
                g.IsStreamOwner = false;
                g.BeginUpdate();
                g.Add(n, "a1.dat");
                g.CommitUpdate();
                Assert.IsTrue(g.TestArchive(true), "Archive test should pass");
                ze = g[1];
                Assert.IsTrue(ze.IsCrypted, "New entry should be encrypted");

                using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
                    string data = r.ReadToEnd();
                    Assert.AreEqual(TestValue, data);
                }
            }
        }
Пример #10
0
        public void EmbeddedArchive()
        {
            var memStream = new MemoryStream();
            using (var f = new ZipFile(memStream))
            {
                f.IsStreamOwner = false;

                var m = new StringMemoryDataSource("0000000");
                f.BeginUpdate(new MemoryArchiveStorage());
                f.Add(m, "a.dat");
                f.Add(m, "b.dat");
                f.CommitUpdate();
                Assert.IsTrue(f.TestArchive(true));
            }

            var rawArchive = memStream.ToArray();
            var pseudoSfx = new byte[1049 + rawArchive.Length];
            Array.Copy(rawArchive, 0, pseudoSfx, 1049, rawArchive.Length);

            memStream = new MemoryStream(pseudoSfx);
            using (var f = new ZipFile(memStream))
            {
                for (var index = 0; index < f.Count; ++index)
                {
                    var entryStream = f.GetInputStream(index);
                    var data = new MemoryStream();
                    StreamUtils.Copy(entryStream, data, new byte[128]);

                    var bytes = data.ToArray();
                    string contents = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    Assert.AreEqual("0000000", contents);
                }
            }
        }