private static void _DotNetZip_CreateZip(List <string> filesToZip,
                                                 EncryptionAlgorithm encryption,
                                                 string password,
                                                 string comment,
                                                 string zipFileToCreate,
                                                 bool nonSeekable)
        {
            // Want to test the library when saving to non-seekable output streams.  Like
            // stdout or ASPNET's Response.OutputStream.  This simulates it.
            if (nonSeekable)
            {
                using (var outStream = new Ionic.Zip.Tests.NonSeekableOutputStream(File.Create(zipFileToCreate)))
                {
                    using (ZipFile zip1 = new ZipFile())
                    {
                        zip1.Encryption = encryption;
                        if (zip1.Encryption != EncryptionAlgorithm.None)
                        {
                            zip1.Password = password;
                        }

                        zip1.AddFiles(filesToZip, "");
                        zip1.Comment = comment;
                        zip1.Save(outStream);
                    }
                }
            }
            else
            {
                using (ZipFile zip1 = new ZipFile())
                {
                    zip1.Encryption = encryption;
                    if (zip1.Encryption != EncryptionAlgorithm.None)
                    {
                        zip1.Password = password;
                    }

                    zip1.AddFiles(filesToZip, "");
                    zip1.Comment = comment;
                    zip1.Save(zipFileToCreate);
                }
            }
        }
Exemple #2
0
        public void Error_LockedFile_wi13903()
        {
            TestContext.WriteLine("==Error_LockedFile_wi13903()");
            string fname = Path.GetRandomFileName();

            TestContext.WriteLine("create file {0}", fname);
            TestUtilities.CreateAndFillFileText(fname, _rnd.Next(10000) + 5000);
            string zipFileToCreate = "wi13903.zip";

            var zipErrorHandler = new EventHandler <ZipErrorEventArgs>((sender, e) =>
            {
                TestContext.WriteLine("Error reading entry {0}", e.CurrentEntry);
                TestContext.WriteLine("  (this was expected)");
                e.CurrentEntry.ZipErrorAction = ZipErrorAction.Skip;
            });

            // lock the file
            TestContext.WriteLine("lock file {0}", fname);
            using (var s = System.IO.File.Open(fname,
                                               FileMode.Open,
                                               FileAccess.Read,
                                               FileShare.None))
            {
                using (var rawOut = File.Create(zipFileToCreate))
                {
                    using (var nonSeekableOut = new Ionic.Zip.Tests.NonSeekableOutputStream(rawOut))
                    {
                        TestContext.WriteLine("create zip file {0}", zipFileToCreate);
                        using (var zip = new ZipFile())
                        {
                            zip.ZipError += zipErrorHandler;
                            zip.AddFile(fname);
                            // should trigger a read error,
                            // which should be skipped. Result will be
                            // a zero-entry zip file.
                            zip.Save(nonSeekableOut);
                        }
                    }
                }
            }
            TestContext.WriteLine("all done, A-OK");
        }
Exemple #3
0
        public void Error_LockedFile_wi13903()
        {
            TestContext.WriteLine("==Error_LockedFile_wi13903()");
            string fname = Path.GetRandomFileName();
            TestContext.WriteLine("create file {0}", fname);
            TestUtilities.CreateAndFillFileText(fname, _rnd.Next(10000) + 5000);
            string zipFileToCreate = "wi13903.zip";

            var zipErrorHandler = new EventHandler<ZipErrorEventArgs>( (sender, e)  =>
                {
                    TestContext.WriteLine("Error reading entry {0}", e.CurrentEntry);
                    TestContext.WriteLine("  (this was expected)");
                    e.CurrentEntry.ZipErrorAction = ZipErrorAction.Skip;
                });

            // lock the file
            TestContext.WriteLine("lock file {0}", fname);
            using (var s = System.IO.File.Open(fname,
                                               FileMode.Open,
                                               FileAccess.Read,
                                               FileShare.None))
            {
                using (var rawOut = File.Create(zipFileToCreate))
                {
                    using (var nonSeekableOut = new Ionic.Zip.Tests.NonSeekableOutputStream(rawOut))
                    {
                        TestContext.WriteLine("create zip file {0}", zipFileToCreate);
                        using (var zip = new ZipFile())
                        {
                            zip.ZipError += zipErrorHandler;
                            zip.AddFile(fname);
                            // should trigger a read error,
                            // which should be skipped. Result will be
                            // a zero-entry zip file.
                            zip.Save(nonSeekableOut);
                        }
                    }
                }
            }
            TestContext.WriteLine("all done, A-OK");
        }
Exemple #4
0
        private static ZipOutputStream GetZipOutputStream(bool seekable, int fileOutputOption, string zipFileToCreate)
        {
            if (fileOutputOption == 0)
            {
                Stream raw = File.Create(zipFileToCreate);
                // conditionally use a non-seekable output stream
                if (!seekable)
                    raw = new Ionic.Zip.Tests.NonSeekableOutputStream(raw);

                return new ZipOutputStream(raw);
            }

            return FileSystemZip.CreateOutputStream(zipFileToCreate);
        }
Exemple #5
0
        private void _Internal_AddEntry_WriteDelegate(string[] files,
                                                      EncryptionAlgorithm crypto,
                                                      bool seekable,
                                                      int cycle,
                                                      string format,
                                                      int ignored)
        {
            int bufferSize = 2048;
            byte[] buffer = new byte[bufferSize];
            int n;

            for (int k = 0; k < compLevels.Length; k++)
            {
                string zipFileToCreate = Path.Combine(TopLevelDir, String.Format(format, compLevels[k].ToString()));
                string password = TestUtilities.GenerateRandomPassword();

                using (var zip = new ZipFile())
                {
                    TestContext.WriteLine("=================================");
                    TestContext.WriteLine("Creating {0}...", Path.GetFileName(zipFileToCreate));
                    TestContext.WriteLine("Encryption({0})  Compression({1})  pw({2})",
                                          crypto.ToString(), compLevels[k].ToString(), password);

                    zip.Password = password;
                    zip.Encryption = crypto;
                    zip.CompressionLevel = compLevels[k];

                    foreach (var file in files)
                    {
                        zip.AddEntry(file, (name, output) =>
                            {
                                using (var input = File.OpenRead(name))
                                {
                                    while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
                                    {
                                        output.Write(buffer, 0, n);
                                    }
                                }
                            });
                    }


                    if (!seekable)
                    {
                        // conditionally use a non-seekable output stream
                        using (var raw = File.Create(zipFileToCreate))
                        {
                            using (var ns = new Ionic.Zip.Tests.NonSeekableOutputStream(raw))
                            {
                                zip.Save(ns);
                            }
                        }
                    }
                    else
                        zip.Save(zipFileToCreate);
                }

                BasicVerifyZip(Path.GetFileName(zipFileToCreate), password);

                Assert.AreEqual<int>(files.Length, TestUtilities.CountEntries(zipFileToCreate),
                                     "Trial ({0},{1}): The zip file created has the wrong number of entries.", cycle, k);
            }
        }
        public void ShellApplication_Unzip_NonSeekableOutput()
        {
            // get a set of files to zip up
            string subdir = Path.Combine(TopLevelDir, "files");
            string[] filesToZip;
            Dictionary<string, byte[]> checksums;
            CreateFilesAndChecksums(subdir, out filesToZip, out checksums);

            var script = GetScript("VbsUnzip-ShellApp.vbs");

            int i = 0;
            foreach (var compLevel in compLevels)
            {
                // create and fill the directories
                string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("ShellApplication_Unzip_NonSeekableOutput.{0}.zip", i));
                string extractDir = Path.Combine(TopLevelDir, String.Format("extract.{0}", i));

                // Create the zip archive
                //Directory.SetCurrentDirectory(TopLevelDir);

                // Want to test the library when saving to non-seekable output streams.  Like
                // stdout or ASPNET's Response.OutputStream.  This simulates it.
                using (var rawOut = System.IO.File.Create(zipFileToCreate))
                {
                    using (var nonSeekableOut = new Ionic.Zip.Tests.NonSeekableOutputStream(rawOut))
                    {
                        using (ZipFile zip1 = new ZipFile())
                        {
                            zip1.CompressionLevel = (Ionic.Zlib.CompressionLevel)compLevel;
                            for (int j = 0; j < filesToZip.Length; j++)
                                zip1.AddItem(filesToZip[j], "files");
                            zip1.Save(nonSeekableOut);
                        }
                    }
                }

                // Verify the number of files in the zip
                Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), filesToZip.Length,
                                     "Incorrect number of entries in the zip file.");

                // run the unzip script
                this.Exec(cscriptExe,
                          String.Format("\"{0}\" {1} {2}", script,
                          Path.GetFileName(zipFileToCreate),
                          Path.GetFileName(extractDir)));

                // check the files in the extract dir
                VerifyChecksums(Path.Combine(extractDir, "files"), filesToZip, checksums);

                VerifyFileTimes(Path.Combine(extractDir, "files"), filesToZip,
                                false, false, 20000 * 1000);  // 2s threshold for DOS times
                i++;
            }
        }
        public void Winzip_Unzip_Password_NonSeekableOutput()
        {
            if (!WinZipIsPresent)
                throw new Exception("[Winzip_Unzip_Password_NonSeekableOutput] : winzip is not present");


            // create a bunch of files
            string subdir = "fodder";
            string[] filesToZip;
            Dictionary<string, byte[]> checksums;
            CreateFilesAndChecksums(subdir, out filesToZip, out checksums);

            var compressionLevels = Enum.GetValues(typeof(Ionic.Zlib.CompressionLevel));
            int i = 0;
            foreach (var compLevel in compressionLevels)
            {
                string zipFileToCreate =
                    Path.Combine(TopLevelDir,
                                 String.Format("Winzip_Unzip_Pwd_NonSeek.{0}.zip",
                                               i));
                string extractDir = "extract" + i.ToString();
                string password = Path.GetRandomFileName();

                // Want to test the library when saving to non-seekable
                // output streams.  Like stdout or ASPNET's
                // Response.OutputStream.  This simulates it.
                using (var rawOut = System.IO.File.Create(zipFileToCreate))
                {
                    using (var nonSeekableOut = new Ionic.Zip.Tests.NonSeekableOutputStream(rawOut))
                    {
                        // Create the zip archive
                        using (ZipFile zip1 = new ZipFile())
                        {
                            zip1.CompressionLevel = (Ionic.Zlib.CompressionLevel)compLevel;
                            zip1.Password = password;
                            zip1.AddFiles(filesToZip, "files");
                            zip1.Save(nonSeekableOut);
                        }
                    }
                }

                // Verify the number of files in the zip
                Assert.AreEqual<int>(filesToZip.Length,
                                     TestUtilities.CountEntries(zipFileToCreate),
                                     "Incorrect number of entries in the zip file.");

                // now, test the zip
                // eg, wzunzip.exe -t test.zip
                string wzunzipOut = this.Exec(wzunzip, String.Format("-t -s{0} {1}",
                                                                     password, zipFileToCreate));
                TestContext.WriteLine("{0}", wzunzipOut);

                Assert.IsTrue(wzunzipOut.Contains("No errors"));
                Assert.IsFalse(wzunzipOut.Contains("At least one error was detected"));

                // extract the zip
                Directory.CreateDirectory(extractDir);
                // eg, wzunzip.exe -d -yx -sPassword  test.zip  <extractdir>
                wzunzipOut = this.Exec(wzunzip, String.Format("-d -yx -s{0} {1} {2}",
                                                             password, zipFileToCreate, extractDir));
                Assert.IsFalse(wzunzipOut.Contains("skipping"));
                Assert.IsFalse(wzunzipOut.Contains("incorrect"));

                // check the files in the extract dir
                VerifyChecksums(Path.Combine(extractDir, "files"), filesToZip, checksums);

                i++;
            }
        }
        public void SevenZip_Unzip_Password_NonSeekableOutput()
        {
            if (!SevenZipIsPresent)
                throw new Exception("[7z_Unzip_Password_NonSeekableOutput] : SevenZip is not present");

            string subdir = Path.Combine(TopLevelDir, "files");

            string[] filesToZip;
            Dictionary<string, byte[]> checksums = null;
            CreateFilesAndChecksums(subdir, out filesToZip, out checksums);
            //CreateFilesAndChecksums(subdir, 2, 32, out filesToZip, out checksums);

#if NOT
            // debugging
            Directory.CreateDirectory(subdir);
            DateTime atMidnight = new DateTime(DateTime.Now.Year,
                                               DateTime.Now.Month,
                                               DateTime.Now.Day,
                                               11,11,11);
            filesToZip = new String[2];
            for (int z=0; z < 2; z++)
            {
                string fname = Path.Combine(subdir, String.Format("file{0:D3}.txt", z));
                File.WriteAllText(fname, "12341234123412341234123412341234");
                File.SetLastWriteTime(fname, atMidnight);
                File.SetLastAccessTime(fname, atMidnight);
                File.SetCreationTime(fname, atMidnight);
                filesToZip[z]= fname;
            }
#endif
            TestContext.WriteLine("Test Unzip with 7zip");
            TestContext.WriteLine("============================================");

            // marker file
            // using (File.Create(Path.Combine(TopLevelDir, "DotNetZip-" + ZipFile.LibraryVersion.ToString()))) ;

            int i = 0;
            foreach (var compLevel in compLevels)
            {
                TestContext.WriteLine("---------------------------------");
                TestContext.WriteLine("Trial {0}", i);
                TestContext.WriteLine("CompressionLevel = {0}", compLevel);
                string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("7z_Unzip_Password_NonSeekableOutput.{0}.zip", i));
                string password = Path.GetRandomFileName();
                //string password = "******";
                string extractDir = Path.Combine(TopLevelDir, String.Format("extract.{0}", i));

                TestContext.WriteLine("Password = {0}", password);

                // Create the zip archive with DotNetZip
                //Directory.SetCurrentDirectory(TopLevelDir);
                // Want to test the library when saving to non-seekable output streams.  Like
                // stdout or ASPNET's Response.OutputStream.  This simulates it.
                using (var rawOut = System.IO.File.Create(zipFileToCreate))
                {
                    using (var nonSeekableOut = new Ionic.Zip.Tests.NonSeekableOutputStream(rawOut))
                    {
                        using (ZipFile zip1 = new ZipFile())
                        {
                            zip1.CompressionLevel = (Ionic.Zlib.CompressionLevel)compLevel;
                            zip1.Password = password;
                            zip1.AddFiles(filesToZip, "files");
                            zip1.Save(nonSeekableOut);
                        }
                    }
                }

                // Verify the number of files in the zip
                Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), filesToZip.Length,
                                     "Incorrect number of entries in the zip file.");

                // unpack the zip archive via 7z.exe
                Directory.CreateDirectory(extractDir);
                this.Exec(sevenZip, String.Format("x -o{0} -p{1} {2}",
                                                  Path.GetFileName(extractDir),
                                                  password,
                                                  zipFileToCreate));

                // check the files in the extract dir
                //Directory.SetCurrentDirectory(TopLevelDir);

                VerifyChecksums(Path.Combine(extractDir, "files"), filesToZip, checksums);
                i++;
            }
        }
        private static void _DotNetZip_CreateZip(List<string> filesToZip,
                                                 EncryptionAlgorithm encryption,
                                                 string password,
                                                 string comment,
                                                 string zipFileToCreate,
                                                 bool nonSeekable)
        {
            // Want to test the library when saving to non-seekable output streams.  Like
            // stdout or ASPNET's Response.OutputStream.  This simulates it.
            if (nonSeekable)
            {
                using (var outStream = new Ionic.Zip.Tests.NonSeekableOutputStream(File.Create(zipFileToCreate)))
                {
                    using (ZipFile zip1 = new ZipFile())
                    {
                        zip1.Encryption = encryption;
                        if (zip1.Encryption != EncryptionAlgorithm.None)
                            zip1.Password = password;

                        zip1.AddFiles(filesToZip, "");
                        zip1.Comment = comment;
                        zip1.Save(outStream);
                    }
                }
            }
            else
            {
                using (ZipFile zip1 = new ZipFile())
                {
                    zip1.Encryption = encryption;
                    if (zip1.Encryption != EncryptionAlgorithm.None)
                        zip1.Password = password;

                    zip1.AddFiles(filesToZip, "");
                    zip1.Comment = comment;
                    zip1.Save(zipFileToCreate);
                }
            }
        }