Provides a stream metaphor for generating zip files.

This class provides an alternative programming model to the one enabled by the ZipFile class. Use this when creating zip files, as an alternative to the ZipFile class, when you would like to use a Stream class to write the zip file.

Some designs require a writable stream for output. This stream can be used to produce a zip file, as it is written.

Both the ZipOutputStream class and the ZipFile class can be used to create zip files. Both of them support many of the common zip features, including Unicode, different compression levels, and ZIP64. For example, when creating a zip file via calls to the PutNextEntry() and Write() methods on the ZipOutputStream class, the caller is responsible for opening the file, reading the bytes from the file, writing those bytes into the ZipOutputStream, setting the attributes on the ZipEntry, and setting the created, last modified, and last accessed timestamps on the zip entry. All of these things are done automatically by a call to ZipFile.AddFile(). For this reason, the ZipOutputStream is generally recommended for when your application wants to emit the arbitrary data, not necessarily data from a filesystem file, directly into a zip file.

Aside from the differences in programming model, there are other differences in capability between the two classes.

ZipFile can be used to read and extract zip files, in addition to creating zip files. ZipOutputStream cannot read zip files. If you want to use a stream to read zip files, check out the ZipInputStream class. ZipOutputStream does not support the creation of segmented or spanned zip files. ZipOutputStream cannot produce a self-extracting archive.
상속: Stream
예제 #1
0
            /// Raises exception on failure.
            /// On failure, existing file is untouched.
            public void Commit()
            {
                if (m_finished)
                {
                    return;
                }
                m_finished = true;

                if (m_zipstream != null)
                {
                    m_zipstream.Dispose();
                    m_zipstream = null;
                }

                string previous = m_destination + "_previous";

                Destroy(previous);
                // Don't destroy previous version until we know the new version is in place.
                try { Rename(m_destination, previous); }
                // The *NotFound exceptions are benign; they happen when writing a new file.
                // Let the other IOExceptions bubble up; they probably indicate some problem
                catch (FileNotFoundException) {}
                catch (DirectoryNotFoundException) {}
                Rename(m_temporaryPath, m_destination);
                Destroy(previous);
            }
예제 #2
0
        /// <summary>
        /// Compress a given file and delete the original file. Automatically rename the file to name.zip.
        /// </summary>
        /// <param name="textPath">Path of the original file</param>
        /// <param name="deleteOriginal">Boolean flag to delete the original file after completion</param>
        /// <returns>String path for the new zip file</returns>
        public static string Zip(string textPath, bool deleteOriginal = true)
        {
            var buffer = new byte[4096];
            var zipPath = textPath.Replace(".csv", ".zip").Replace(".txt", ".zip");

            using (var stream = new ZipOutputStream(File.Create(zipPath)))
            {
                stream.PutNextEntry(Path.GetFileName(textPath));

                // copy everything from the file to the zip
                using (var fs = File.OpenRead(textPath))
                {
                    int sourceBytes;
                    do
                    {
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
                        stream.Write(buffer, 0, sourceBytes);
                    }
                    while (sourceBytes > 0);
                }
            }

            //Delete the old text file:
            if (deleteOriginal) File.Delete(textPath);
            return zipPath;
        }
예제 #3
0
 /********************************************************
 * CLASS METHODS
 *********************************************************/
 /// <summary>
 /// 
 /// </summary>
 /// <param name="zipPath"></param>
 /// <param name="filenamesAndData"></param>
 /// <returns></returns>
 public static bool Zip(string zipPath, System.Collections.Generic.Dictionary<string, string> filenamesAndData)
 {
     var success = true;
     var buffer = new byte[4096];
     try
     {
         using (var stream = new ZipOutputStream(System.IO.File.Create(zipPath)))
         {
             foreach (var filename in filenamesAndData.Keys)
             {
                 var file = filenamesAndData[filename].GetBytes();
                 var entry = stream.PutNextEntry(filename);
                 using (var ms = new System.IO.MemoryStream(file))
                 {
                     int sourceBytes;
                     do
                     {
                         sourceBytes = ms.Read(buffer, 0, buffer.Length);
                         stream.Write(buffer, 0, sourceBytes);
                     }
                     while (sourceBytes > 0);
                 }
             }
             stream.Flush();
             stream.Close();
         }
     }
     catch (System.Exception err)
     {
         System.Console.WriteLine("Compression.ZipData(): " + err.Message);
         success = false;
     }
     return success;
 }
예제 #4
0
		public void Dispose()
		{
			if (z != null)
			{
				z.Dispose();
				z = null;
			}
		}
예제 #5
0
 public ZipWriteOperator(string targetFile)
 {
     zip = new ZipOutputStream(new FileStream(targetFile, FileMode.Create))
     {
         AlternateEncodingUsage = ZipOption.AsNecessary,
         AlternateEncoding = Encoding.Unicode
     };
 }
예제 #6
0
 public ArchiveWriter(string filename, Stats stats)
 {
     this.stats = stats;
     this.baseStream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None, Settings.FileStreamBufferSize);
     this.zipStream = new ZipOutputStream(this.baseStream);
     this.zipStream.EnableZip64 = Zip64Option.AsNecessary;
     this.zipStream.ParallelDeflateThreshold = 0; // Force enabled
     this.zipStream.UseUnicodeAsNecessary = true;
 }
예제 #7
0
		public IonicZipWriter(string path, int compressionlevel)
		{
			level = compressionlevel;
			z = new ZipOutputStream(path)
			{
				EnableZip64 = Zip64Option.Never,
				CompressionLevel = (Ionic.Zlib.CompressionLevel)level
			};
			z.CompressionMethod = CompressionMethod.Deflate;
	}
예제 #8
0
 /// <summary>
 /// Writes a zip file
 /// </summary>
 /// <param name="zipPath">string - the path to the zip file to be written.</param>
 /// <param name="internalFilename">string - the name of the file within the zip file.  It can be a path</param>
 /// <param name="data">string - the data to be written into the zip file.</param>
 /// <returns>nothing</returns>
 public static async Task ZipAsync(string zipPath, string internalFilename, string data)
 {
     byte[] buf = Encoding.ASCII.GetBytes(data);
     using (var fs = File.Create(zipPath, buf.Length))
     {
         using (var s = new ZipOutputStream(fs))
         {
             s.PutNextEntry(internalFilename);
             await s.WriteAsync(buf, 0, buf.Length);
         }
     }
 }
예제 #9
0
            public AtomicWriter(string path)
            {
                m_destination   = path;
                m_temporaryPath = path + "_part";
                Destroy(m_temporaryPath);

                bool useZip;

                switch (DevOptions.I.PreferredTiltFormat)
                {
                case TiltFormat.Directory:
                    useZip = false;
                    break;

                case TiltFormat.Inherit:
                    useZip = !Directory.Exists(path);
                    break;

                default:
                case TiltFormat.Zip:
                    useZip = true;
                    break;
                }
                if (useZip)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(m_temporaryPath));
                    FileStream tmpfs  = new FileStream(m_temporaryPath, FileMode.Create, FileAccess.Write);
                    var        header = new TiltZipHeader
                    {
                        sentinel      = TILT_SENTINEL,
                        headerSize    = HEADER_SIZE,
                        headerVersion = HEADER_VERSION,
                    };
                    WriteTiltZipHeader(tmpfs, header);
                    m_zipstream = new ZipLibrary.ZipOutputStream(tmpfs);
#if USE_DOTNETZIP
                    // Ionic.Zip documentation says that using compression level None produces
                    // zip files that cannot be opened with the default OSX zip reader.
                    // But compression _method_ none seems fine?
                    m_zipstream.CompressionMethod = ZipLibrary.CompressionMethod.None;
                    m_zipstream.EnableZip64       = ZipLibrary.Zip64Option.Never;
#else
                    m_zipstream.SetLevel(0); // no compression
                    // Since we don't have size info up front, it conservatively assumes 64-bit.
                    // We turn it off to maximize compatibility with wider ecosystem (eg, osx unzip).
                    m_zipstream.UseZip64 = ZipLibrary.UseZip64.Off;
#endif
                }
                else
                {
                    Directory.CreateDirectory(m_temporaryPath);
                }
            }
예제 #10
0
        private void ExportDirectory(ZipOutputStream zip, Directory directory)
        {
            foreach (var file in directory.GetFiles())
            {
                zip.PutNextEntry(file.Url.Substring(rootPath.Length));
                fileSystem.ReadFileContents(file.Url, zip);
            }

            foreach (var subDirectory in directory.GetDirectories())
            {
                ExportDirectory(zip, subDirectory);
            }
        }
예제 #11
0
		public int Compress(PointCloudTile tile, byte[] uncompressedBuffer, int count, byte[] compressedBuffer)
		{
			MemorableMemoryStream compressedStream = new MemorableMemoryStream(compressedBuffer);

			using (ZipOutputStream zipStream = new ZipOutputStream(compressedStream, true))
			{
				zipStream.CompressionMethod = Ionic.Zip.CompressionMethod.Deflate;
				zipStream.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;

				zipStream.PutNextEntry("a");
				zipStream.Write(uncompressedBuffer, 0, count);
			}

			return (int)compressedStream.MaxPosition;
		}
예제 #12
0
            public void Rollback()
            {
                if (m_finished)
                {
                    return;
                }
                m_finished = true;

                if (m_zipstream != null)
                {
                    m_zipstream.Dispose();
                    m_zipstream = null;
                }

                Destroy(m_temporaryPath);
            }
예제 #13
0
파일: ZIPUnpacker.cs 프로젝트: darkstar/gus
        public void PackFiles(Stream strm, List<PackFileEntry> filesToPack, Callbacks callbacks)
        {
            SetupZIPParams();
            using (ZipOutputStream zstrm = new ZipOutputStream(strm, false))
            {
                // always use multithreaded compression
                zstrm.ParallelDeflateThreshold = 0;

                foreach (PackFileEntry pfe in filesToPack)
                {
                    zstrm.PutNextEntry(pfe.relativePathName);
                    byte[] data = callbacks.ReadData(pfe.relativePathName);
                    zstrm.Write(data, 0, data.Length);
                }
            }
            // stream is closed automatically
        }
예제 #14
0
        protected void btnExport_Click(object sender, EventArgs e)
        {
            var zipFileName = System.IO.Path.GetFileName(rootPath) + ".zip";

            var directory = Directory.New(fileSystem.GetDirectory(rootPath), null, Engine.Resolve<IDependencyInjector>());

            Response.Clear();
            Response.BufferOutput = false;
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + zipFileName);

            using (var zip = new ZipOutputStream(Response.OutputStream))
            {
                ExportDirectory(zip, directory);
            }

            Response.End();
        }
        public void WriteCompressedZip(string filepath)
        {
            ZipOutputStream outstream = new ZipOutputStream(filepath);
            outstream.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
            BinaryFormatter formatter = new BinaryFormatter();
            outstream.PutNextEntry("dump");

            //Logger.WriteLineTimed("Started compressing search dump");
            DateTime startTime = DateTime.Now;

            formatter.Serialize(outstream, searchDump.StartAddress);
            formatter.Serialize(outstream, searchDump.EndAddress);
            outstream.Write(searchDump.mem, 0, (int)(searchDump.EndAddress - searchDump.StartAddress));

            DateTime endTime = DateTime.Now;
            //Logger.WriteLineTimed("Finished compressing search dump in " + (new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds));

            outstream.PutNextEntry("list");

            //Logger.WriteLineTimed("Started copying search list");
            startTime = DateTime.Now;

            List<UInt32> copy = new List<uint>(resultsList);

            endTime = DateTime.Now;
            //Logger.WriteLineTimed("Finished copying search list in " + (new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds));

            //Logger.WriteLineTimed("Started compressing search list");
            startTime = DateTime.Now;

            formatter.Serialize(outstream, resultsList);

            endTime = DateTime.Now;
            //Logger.WriteLineTimed("Finished compressing search list in " + (new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds));

            outstream.Close();
            outstream.Dispose();
        }
예제 #16
0
        private void _ZOS_z64Over65534Entries
            (Zip64Option z64option,
             EncryptionAlgorithm encryption,
             Ionic.Zlib.CompressionLevel compression)
        {
            TestContext.WriteLine("_ZOS_z64Over65534Entries hello: {0}",
                                  DateTime.Now.ToString("G"));
            int fileCount = _rnd.Next(14616) + 65536;
            //int fileCount = _rnd.Next(146) + 5536;
            TestContext.WriteLine("entries: {0}", fileCount);
            var txrxLabel =
                String.Format("ZOS  #{0} 64({3}) E({1}) C({2})",
                              fileCount,
                              encryption.ToString(),
                              compression.ToString(),
                              z64option.ToString());

            TestContext.WriteLine("label: {0}", txrxLabel);
            string zipFileToCreate =
                String.Format("ZOS.Zip64.over65534.{0}.{1}.{2}.zip",
                              z64option.ToString(), encryption.ToString(),
                              compression.ToString());

            TestContext.WriteLine("zipFileToCreate: {0}", zipFileToCreate);

            _txrx = TestUtilities.StartProgressMonitor(zipFileToCreate,
                                                       txrxLabel, "starting up...");

            TestContext.WriteLine("generating {0} entries ", fileCount);
            _txrx.Send("pb 0 max 3"); // 2 stages: Write, Count, Verify
            _txrx.Send("pb 0 value 0");

            string password = Path.GetRandomFileName();

            string statusString = String.Format("status Encryption:{0} Compression:{1}",
                                                encryption.ToString(),
                                                compression.ToString());

            _txrx.Send(statusString);

            int dirCount = 0;

            using (FileStream fs = File.Create(zipFileToCreate))
            {
                using (var output = new ZipOutputStream(fs))
                {
                    _txrx.Send("test " + txrxLabel);
                    System.Threading.Thread.Sleep(400);
                    _txrx.Send("pb 1 max " + fileCount);
                    _txrx.Send("pb 1 value 0");

                    output.Password = password;
                    output.Encryption = encryption;
                    output.CompressionLevel = compression;
                    output.EnableZip64 = z64option;
                    for (int k = 0; k < fileCount; k++)
                    {
                        if (_rnd.Next(7) == 0)
                        {
                            // make it a directory
                            string entryName = String.Format("{0:D4}/", k);
                            output.PutNextEntry(entryName);
                            dirCount++;
                        }
                        else
                        {
                            string entryName = String.Format("{0:D4}.txt", k);
                            output.PutNextEntry(entryName);

                            // only a few entries are non-empty
                            if (_rnd.Next(18) == 0)
                            {
                                var block = TestUtilities.GenerateRandomAsciiString();
                                string content = String.Format("This is the content for entry #{0}.\n", k);
                                int n = _rnd.Next(4) + 1;
                                for (int j=0; j < n; j++)
                                    content+= block;

                                byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content);
                                output.Write(buffer, 0, buffer.Length);
                            }
                        }
                        if (k % 1024 == 0)
                            _txrx.Send(String.Format("status saving ({0}/{1}) {2:N0}%",
                                                     k, fileCount,
                                                     ((double)k) / (0.01 * fileCount)));
                        else if (k % 256 == 0)
                            _txrx.Send("pb 1 value " + k);
                    }
                }
            }

            _txrx.Send("pb 1 max 1");
            _txrx.Send("pb 1 value 1");
            _txrx.Send("pb 0 step");

            System.Threading.Thread.Sleep(400);

            TestContext.WriteLine("Counting entries ... " + DateTime.Now.ToString("G"));
            _txrx.Send("status Counting entries...");
            Assert.AreEqual<int>
                (fileCount - dirCount,
                 TestUtilities.CountEntries(zipFileToCreate),
                 "{0}: The zip file created has the wrong number of entries.",
                 zipFileToCreate);
            _txrx.Send("pb 0 step");
            System.Threading.Thread.Sleep(140);

            // basic verify. The output is really large, so we pass emitOutput=false .
            _txrx.Send("status Verifying...");
            TestContext.WriteLine("Verifying ... " + DateTime.Now.ToString("G"));
            _numExtracted = 0;
            _numFilesToExtract = fileCount;
            _txrx.Send("pb 1 max " + fileCount);
            System.Threading.Thread.Sleep(200);
            _txrx.Send("pb 1 value 0");
            BasicVerifyZip(zipFileToCreate, password, false, Streams_ExtractProgress);
            _txrx.Send("pb 0 step");
            System.Threading.Thread.Sleep(800);
            TestContext.WriteLine("Done ... " + DateTime.Now.ToString("G"));
        }
예제 #17
0
 public void ZOS_Create_ZeroByteEntry_wi12964()
 {
     using (var zip = new Ionic.Zip.ZipOutputStream(new MemoryStream()))
     {
         zip.PutNextEntry("EmptyFile.txt");
         zip.Write(new byte[1], 0, 0);
     }
 }
예제 #18
0
 public void ZOS_Create_WithComment_wi10339()
 {
     string zipFileToCreate = "ZOS_Create_WithComment_wi10339.zip";
     using (var fs = File.Create(zipFileToCreate))
     {
         using (var output = new ZipOutputStream(fs))
         {
             output.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
             output.Comment = "Cheeso is the man!";
             string entryName = String.Format("entry{0:D4}.txt", _rnd.Next(10000));
             output.PutNextEntry(entryName);
             string content = "This is the content for the entry.";
             byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content);
             output.Write(buffer, 0, buffer.Length);
         }
     }
 }
예제 #19
0
        public void ZIS_ZOS_VaryCompression()
        {
            string resourceDir = Path.Combine(SourceDir, TestUtilities.GetBinDir("Zip.Portable.Tests"), "Resources");
            var filesToAdd = Directory.GetFiles(resourceDir);

            Func<int, int, bool> chooseCompression = (ix, cycle) => {
                var name = Path.GetFileName(filesToAdd[ix]);
                switch (cycle)
                {
                    case 0:
                        return !(name.EndsWith(".zip") ||
                                 name.EndsWith(".docx") ||
                                 name.EndsWith(".xslx"));
                    case 1:
                        return ((ix%2)==0);

                    default:
                        return (ix == filesToAdd.Length - 1);
                }
            };

            // Three cycles - three different ways to vary compression
            for (int k=0; k < 3; k++)
            {
                string zipFileToCreate = String.Format("VaryCompression-{0}.zip", k);

                TestContext.WriteLine("");
                TestContext.WriteLine("Creating zip, cycle {0}", k);
                using (var fileStream = File.OpenWrite(zipFileToCreate))
                {
                    using (var zos = new ZipOutputStream(fileStream, true))
                    {
                        for (int i=0; i < filesToAdd.Length; i++)
                        {
                            var file = filesToAdd[i];
                            var shortName = Path.GetFileName(file);
                            bool compress = chooseCompression(i, k);

                            if (compress)
                                zos.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
                            else
                                zos.CompressionLevel = Ionic.Zlib.CompressionLevel.None;

                            zos.PutNextEntry(shortName);
                            using (var input = File.OpenRead(file))
                            {
                                CopyStream(input, zos);
                            }
                        }
                    }
                }

                TestContext.WriteLine("");
                TestContext.WriteLine("Extracting cycle {0}", k);
                string extractDir = "extract-" + k;
                Directory.CreateDirectory(extractDir);
                using (var raw = File.OpenRead(zipFileToCreate))
                {
                    using (var input = new ZipInputStream(raw))
                    {
                        ZipEntry e;
                        while ((e = input.GetNextEntry()) != null)
                        {
                            TestContext.WriteLine("entry: {0}", e.FileName);
                            string outputPath = Path.Combine(extractDir, e.FileName);
                            if (e.IsDirectory)
                            {
                                // create the directory
                                Directory.CreateDirectory(outputPath);
                            }
                            else
                            {
                                // create the file
                                using (var output = File.Create(outputPath))
                                {
                                    CopyStream(input,output);
                                }
                            }
                        }
                    }
                }

                string[] filesUnzipped = Directory.GetFiles(extractDir);
                Assert.AreEqual<int>(filesToAdd.Length, filesUnzipped.Length,
                                     "Incorrect number of files extracted.");

            }
        }
예제 #20
0
        public void ZOS_Create_Encrypt_wi12815()
        {
            string zipFileToCreate =
                "ZOS_Create_Encrypt_wi12815.zip";

            var content = new byte[1789];
            unchecked
            {
                byte b = 0;
                for (var i = 0; i < content.Length; i++, b++)
                {
                    content[i] = b;
                }
            }

            var checkBuffer = new Action<String>(stage =>
            {
                byte b = 0;
                TestContext.WriteLine("Checking buffer ({0})", stage);
                for (var i = 0; i < content.Length; i++, b++)
                {
                    Assert.IsTrue((content[i] == b),
                                  "Buffer was modified.");
                }
            });

            checkBuffer("before");

            using (var fileStream = File.OpenWrite(zipFileToCreate))
            {
                using (var zipStream = new ZipOutputStream(fileStream, true))
                {
                    zipStream.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
                    zipStream.Password = "******";
#if AESCRYPTO
                    zipStream.Encryption = EncryptionAlgorithm.WinZipAes256;
#endif
                    zipStream.PutNextEntry("myentry.myext");
                    zipStream.Write(content, 0, content.Length);
                }
            }

            checkBuffer("after");
        }
예제 #21
0
        public void ZOS_Create_EmptyEntries()
        {
            for (int i = 0; i < crypto.Length; i++)
            {
                for (int j = 0; j < compLevels.Length; j++)
                {
                    string password = Path.GetRandomFileName();

                    for (int k = 0; k < 2; k++)
                    {
                        string zipFileToCreate = String.Format("ZOS_Create_EmptyEntries.Encryption.{0}.{1}.{2}.zip",
                                                               crypto[i].ToString(), compLevels[j].ToString(), k);

                        using (var fs = File.Create(zipFileToCreate))
                        {
                            using (var output = new ZipOutputStream(fs))
                            {
                                byte[] buffer;
                                output.Password = password;
                                output.Encryption = crypto[i];
                                output.CompressionLevel = compLevels[j];
                                output.PutNextEntry("entry1.txt");
                                if (k == 0)
                                {
                                    buffer = System.Text.Encoding.ASCII.GetBytes("This is the content for entry #1.");
                                    output.Write(buffer, 0, buffer.Length);
                                }

                                output.PutNextEntry("entry2.txt");  // this will be zero length
                                output.PutNextEntry("entry3.txt");
                                if (k == 0)
                                {
                                    buffer = System.Text.Encoding.ASCII.GetBytes("This is the content for entry #3.");
                                    output.Write(buffer, 0, buffer.Length);
                                }
                                output.PutNextEntry("entry4.txt");  // this will be zero length
                                output.PutNextEntry("entry5.txt");  // this will be zero length
                            }
                        }

                        BasicVerifyZip(zipFileToCreate, password);

                        Assert.AreEqual<int>(5, TestUtilities.CountEntries(zipFileToCreate),
                                             "Trial ({0},{1}): The zip file created has the wrong number of entries.", i, j);
                    }
                }
            }
        }
예제 #22
0
 public void ZOS_Create_WriteBeforePutNextEntry()
 {
     string zipFileToCreate = "ZOS_Create_WriteBeforePutNextEntry.zip";
     using (var fs = File.Create(zipFileToCreate))
     {
         using (var output = new ZipOutputStream(fs))
         {
             //output.PutNextEntry("entry1.txt");
             byte[] buffer = System.Text.Encoding.ASCII.GetBytes("This is the content for entry #1.");
             output.Write(buffer, 0, buffer.Length);
         }
     }
 }
        private Stream CompressToZip(ItemNameValueCollection entriesPathId)
        {
            var stream = TempStream.Create();
            using (var zip = new ZipOutputStream(stream, true))
            {
                zip.CompressionLevel = CompressionLevel.Level3;
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                zip.AlternateEncoding = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage);

                foreach (var path in entriesPathId.AllKeys)
                {
                    if (Canceled)
                    {
                        zip.Dispose();
                        stream.Dispose();
                        return null;
                    }

                    var counter = 0;
                    foreach (var entryId in entriesPathId[path])
                    {
                        var newtitle = path;

                        File file = null;
                        var convertToExt = string.Empty;

                        if (!string.IsNullOrEmpty(entryId))
                        {
                            file = FileDao.GetFile(entryId);

                            if (file.ContentLength > SetupInfo.AvailableFileSize)
                            {
                                Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize));
                                continue;
                            }

                            if (_files.ContainsKey(file.ID.ToString()))
                            {
                                if (_quotaDocsEdition || FileUtility.InternalExtension.Values.Contains(convertToExt))
                                    convertToExt = _files[file.ID.ToString()];

                                if (!string.IsNullOrEmpty(convertToExt))
                                {
                                    newtitle = FileUtility.ReplaceFileExtension(path, convertToExt);
                                }
                            }
                        }

                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";

                            if (!string.IsNullOrEmpty(entryId))
                            {
                                newtitle = 0 < newtitle.IndexOf('.')
                                               ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix)
                                               : newtitle + suffix;
                            }
                            else
                            {
                                break;
                            }
                        }

                        zip.PutNextEntry(newtitle);

                        if (!string.IsNullOrEmpty(entryId) && file != null)
                        {
                            if (file.ConvertedType != null || !string.IsNullOrEmpty(convertToExt))
                            {
                                //Take from converter
                                try
                                {
                                    using (var readStream = !string.IsNullOrEmpty(convertToExt)
                                                                ? FileConverter.Exec(file, convertToExt)
                                                                : FileConverter.Exec(file))
                                    {
                                        if (readStream != null)
                                        {
                                            readStream.StreamCopyTo(zip);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Error = ex.Message;

                                    Logger.Error(Error, ex);
                                }
                            }
                            else
                            {
                                using (var readStream = FileDao.GetFileStream(file))
                                {
                                    readStream.StreamCopyTo(zip);
                                }
                            }
                        }
                        counter++;
                    }

                    ProgressStep();
                }
                return stream;
            }
        }
예제 #24
0
        private void ExportAllData()
        {
            using (var stream = TempStream.Create())
            {
                var contactDao = _daoFactory.GetContactDao();
                var contactInfoDao = _daoFactory.GetContactInfoDao();
                var dealDao = _daoFactory.GetDealDao();
                var casesDao = _daoFactory.GetCasesDao();
                var taskDao = _daoFactory.GetTaskDao();
                var historyDao = _daoFactory.GetRelationshipEventDao();
                var invoiceItemDao = _daoFactory.GetInvoiceItemDao();

                _totalCount += contactDao.GetAllContactsCount();
                _totalCount += dealDao.GetDealsCount();
                _totalCount += casesDao.GetCasesCount();
                _totalCount += taskDao.GetAllTasksCount();
                _totalCount += historyDao.GetAllItemsCount();
                _totalCount += invoiceItemDao.GetInvoiceItemsCount();

                using (var zipStream = new ZipOutputStream(stream, true))
                {
                    zipStream.PutNextEntry("contacts.csv");
                    var contactData = contactDao.GetAllContacts();
                    var contactInfos = new StringDictionary();
                    contactInfoDao.GetAll()
                                  .ForEach(item =>
                                               {
                                                   var contactInfoKey = String.Format("{0}_{1}_{2}", item.ContactID, (int) item.InfoType, item.Category);
                                                   if (contactInfos.ContainsKey(contactInfoKey))
                                                   {
                                                       contactInfos[contactInfoKey] += "," + item.Data;
                                                   }
                                                   else
                                                   {
                                                       contactInfos.Add(contactInfoKey, item.Data);
                                                   }
                                               });

                    var zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportContactsToCSV(contactData, contactInfos)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("oppotunities.csv");
                    var dealData = dealDao.GetAllDeals();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportDealsToCSV(dealData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("cases.csv");
                    var casesData = casesDao.GetAllCases();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportCasesToCSV(casesData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("tasks.csv");
                    var taskData = taskDao.GetAllTasks();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportTasksToCSV(taskData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("history.csv");
                    var historyData = historyDao.GetAllItems();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportHistoryToCSV(historyData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("products_services.csv");
                    var invoiceItemData = invoiceItemDao.GetAll();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportInvoiceItemsToCSV(invoiceItemData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.Flush();
                    zipStream.Close();

                    stream.Position = 0;
                }

                var assignedURI = _dataStore.SavePrivate(String.Empty, "exportdata.zip", stream, DateTime.Now.AddDays(1));
                Status = assignedURI;
                _notifyClient.SendAboutExportCompleted(_author.ID, assignedURI);
                Complete();
            }
        }
예제 #25
0
        public void ZOS_Create_Directories_Write()
        {
            for (int k = 0; k < 2; k++)
            {
                string zipFileToCreate = String.Format("ZOS_Create_Directories.{0}.zip", k);
                using (var fs = File.Create(zipFileToCreate))
                {
                    using (var output = new ZipOutputStream(fs))
                    {
                        byte[] buffer;
                        output.Encryption = EncryptionAlgorithm.None;
                        output.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                        output.PutNextEntry("entry1/");
                        if (k == 0)
                        {
                            buffer = System.Text.Encoding.ASCII.GetBytes("This is the content for entry #1.");
                            // this should fail
                            output.Write(buffer, 0, buffer.Length);
                        }

                        output.PutNextEntry("entry2/");  // this will be a directory
                        output.PutNextEntry("entry3.txt");
                        if (k == 0)
                        {
                            buffer = System.Text.Encoding.ASCII.GetBytes("This is the content for entry #3.");
                            output.Write(buffer, 0, buffer.Length);
                        }
                        output.PutNextEntry("entry4.txt");  // this will be zero length
                        output.PutNextEntry("entry5.txt");  // this will be zero length
                    }
                }
            }
        }
예제 #26
0
 public DotNetZipZipWriter(Stream outputStream) : this()
 {
     zip = new ZipOutputStream( outputStream ) {EnableZip64 = Zip64Option.AsNecessary};
 }
예제 #27
0
        public void ZOS_Create_DuplicateEntry()
        {
            string zipFileToCreate = "ZOS_Create_DuplicateEntry.zip";

            string entryName = Path.GetRandomFileName();

            using (var fs = File.Create(zipFileToCreate))
            {
                using (var output = new ZipOutputStream(fs))
                {
                    output.PutNextEntry(entryName);
                    output.PutNextEntry(entryName);
                }
            }
        }
예제 #28
0
 public override void SetBaseStream(Stream outputStream)
 {
     zip = new ZipOutputStream(outputStream);
     disposed = false;
 }
예제 #29
0
        public void Password_UnsetEncryptionAfterSetPassword_wi13909_ZOS()
        {
            // Verify that unsetting the Encryption property after
            // setting a Password results in no encryption being used.
            // This method tests ZipOutputStream.
            string unusedPassword = TestUtilities.GenerateRandomPassword();
            int numTotalEntries = _rnd.Next(46)+653;
            string zipFileToCreate = "UnsetEncryption.zip";

            using (FileStream fs = File.Create(zipFileToCreate))
            {
                using (var zos = new ZipOutputStream(fs))
                {
                    zos.Password = unusedPassword;
                    zos.Encryption = EncryptionAlgorithm.None;

                    for (int i=0; i < numTotalEntries; i++)
                    {
                        if (_rnd.Next(7)==0)
                        {
                            string entryName = String.Format("{0:D5}/", i);
                            zos.PutNextEntry(entryName);
                        }
                        else
                        {
                            string entryName = String.Format("{0:D5}.txt", i);
                            zos.PutNextEntry(entryName);
                            if (_rnd.Next(12)==0)
                            {
                                var block = TestUtilities.GenerateRandomAsciiString() + " ";
                                string contentBuffer = String.Format("This is the content for entry {0}", i);
                                int n = _rnd.Next(6) + 2;
                                for (int j=0; j < n; j++)
                                    contentBuffer += block;
                                byte[] buffer = System.Text.Encoding.ASCII.GetBytes(contentBuffer);
                                zos.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }
                }
            }

            BasicVerifyZip(zipFileToCreate);
        }
예제 #30
0
        public void ZipOutputStream_Parallel()
        {
            int _sizeBase = 1024 * 1024;
            int _sizeRange = 256 * 1024;
            //int _sizeBase      = 1024 * 256;
            //int _sizeRange     = 256 * 12;
            var sw = new System.Diagnostics.Stopwatch();
            byte[] buffer = new byte[0x8000];
            int n = 0;
            TimeSpan[] ts = new TimeSpan[2];
            int nFiles = _rnd.Next(8) + 8;
            //int nFiles         = 2;
            string[] filenames = new string[nFiles];
            string dirToZip = Path.Combine(TopLevelDir, "dirToZip");


            string channel = String.Format("ZOS_Parallel{0:000}", _rnd.Next(1000));
            string txrxLabel = "ZipOutputStream Parallel";
            _txrx = TestUtilities.StartProgressMonitor(channel, txrxLabel, "starting up...");

            TestContext.WriteLine("Creating {0} fodder files...", nFiles);
            Directory.CreateDirectory(dirToZip);

            _txrx.Send(String.Format("pb 0 max {0}", nFiles));
            _txrx.Send("pb 0 value 0");

            sw.Start();

            for (int x = 0; x < nFiles; x++)
            {
                string status = String.Format("status Creating file {0}/{1}", x + 1, nFiles);
                _txrx.Send(status);

                filenames[x] = Path.Combine(dirToZip, String.Format("file{0:000}.txt", x));
                using (var output = File.Create(filenames[x]))
                {
                    using (Stream input = new Ionic.Zip.Tests.Utilities.RandomTextInputStream(_sizeBase + _rnd.Next(_sizeRange)))
                    {
                        while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            output.Write(buffer, 0, n);
                        }
                    }
                }
                _txrx.Send("pb 0 step");
            }
            sw.Stop();
            TestContext.WriteLine("file generation took {0}", sw.Elapsed);

            _txrx.Send(String.Format("pb 0 max {0}", crypto.Length));
            _txrx.Send("pb 0 value 0");

            for (int i = 0; i < crypto.Length; i++)
            {
                //int c = i;
                int c = (i + 2) % crypto.Length;

                _txrx.Send(String.Format("pb 1 max {0}", compLevels.Length));
                _txrx.Send("pb 1 value 0");

                for (int j = 0; j < compLevels.Length; j++)
                {
                    string password = Path.GetRandomFileName();

                    // I wanna do 2 cycles if there is compression, so I can compare MT
                    // vs 1T compression.  The first cycle will ALWAYS use the threaded
                    // compression, the 2nd will NEVER use it.  If
                    // CompressionLevel==None, then just do one cycle.
                    //
                    int kCycles = (compLevels[j] == Ionic.Zlib.CompressionLevel.None)
                        ? 1
                        : 2;

                    for (int k = 0; k < kCycles; k++)
                    {
                        // Also, I use Stopwatch to time the compression, and compare.
                        // In light of that, I wanna do one warmup, and then one timed
                        // trial (for t==0..2).  But here again, if CompressionLevel==None, then I
                        // don't want to do a timing comparison, so I don't need 2 trials.
                        // Therefore, in that case, the "warmup" is the only trial I want to do.
                        // So when k==1 and Compression==None, do no cycles at all.
                        //
                        int tCycles = (compLevels[j] == Ionic.Zlib.CompressionLevel.None)
                            ? ((k == 0) ? 1 : 0)
                            : 2;

                        if (k == 0)
                        {
                            _txrx.Send(String.Format("pb 2 max {0}", kCycles * tCycles));
                            _txrx.Send("pb 2 value 0");
                        }

                        for (int t = 0; t < tCycles; t++)
                        {
                            TestContext.WriteLine(new String('-', 72));
                            string zipFileToCreate = String.Format("ZipOutputStream_Parallel.E-{0}.C-{1}.{2}.{3}timed.zip",
                                                                   crypto[c].ToString(), compLevels[j].ToString(),
                                                                   (compLevels[j] == Ionic.Zlib.CompressionLevel.None)
                                                                   ? "NA"
                                                                   : (k == 0) ? "1T" : "MT",
                                                                   (t == 0) ? "not-" : "");

                            TestContext.WriteLine("Trial {0}.{1}.{2}.{3}", i, j, k, t);
                            TestContext.WriteLine("Create zip file {0}", zipFileToCreate);

                            _txrx.Send("status " + zipFileToCreate);

                            sw.Reset();
                            sw.Start();
                            using (var output = new ZipOutputStream(zipFileToCreate))
                            {
                                if (k == 0)
                                    output.ParallelDeflateThreshold = -1L;   // never
                                else
                                    output.ParallelDeflateThreshold = 0L; // always

                                output.Password = password;
                                output.Encryption = crypto[c]; // maybe "None"
                                output.CompressionLevel = compLevels[j];

                                _txrx.Send(String.Format("pb 3 max {0}", nFiles));
                                _txrx.Send("pb 3 value 0");

                                for (int x = 0; x < nFiles; x++)
                                {
                                    output.PutNextEntry(Path.GetFileName(filenames[x]));
                                    using (var input = File.OpenRead(filenames[x]))
                                    {
                                        while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            output.Write(buffer, 0, n);
                                        }
                                    }
                                    _txrx.Send("pb 3 step");
                                }
                            }

                            sw.Stop();
                            ts[k] = sw.Elapsed;
                            TestContext.WriteLine("compression took {0}", ts[k]);

                            //if (t==0)
                            BasicVerifyZip(zipFileToCreate, password);

                            Assert.AreEqual<int>(nFiles, TestUtilities.CountEntries(zipFileToCreate),
                                                 "Trial ({0}.{1}.{2}.{3}): The zip file created has the wrong number of entries.", i, j, k, t);

                            _txrx.Send("pb 2 step");
                        }

                    }

#if NOT_DEBUGGING
                    // parallel is not always faster!
                    if (_sizeBase > 256 * 1024 &&
                        compLevels[j] != Ionic.Zlib.CompressionLevel.None &&
                        compLevels[j] != Ionic.Zlib.CompressionLevel.BestSpeed &&
                        crypto[c] != EncryptionAlgorithm.WinZipAes256  &&
                        crypto[c] != EncryptionAlgorithm.WinZipAes128 )
                        Assert.IsTrue(ts[0]>ts[1], "Whoops! Cycle {0}.{1} (crypto({4}) Comp({5})): Parallel deflate is slower ({2}<{3})",
                                      i, j, ts[0], ts[1],
                                      crypto[c],
                                      compLevels[j]);
#endif
                    _txrx.Send("pb 1 step");
                }
                _txrx.Send("pb 0 step");
            }

            _txrx.Send("stop");
        }
 public ZipContainer(Object o)
 {
     _zf = (o as ZipFile);
     _zos = (o as ZipOutputStream);
     _zis = (o as ZipInputStream);
 }
예제 #32
0
        public void SaveSearch(string filepath, List<UInt32> resultsList, Dump searchDump)
        {
            ZipOutputStream outstream = new ZipOutputStream(filepath);
            outstream.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
            BinaryFormatter formatter = new BinaryFormatter();

            // First entry is the dump
            outstream.PutNextEntry("dump");

            //DateTime start = Logger.WriteLineTimedStarted("compressing search dump");

            // Must put the addresses first, so that it can derive the right number of bytes to read for the dump
            formatter.Serialize(outstream, searchDump.StartAddress);
            formatter.Serialize(outstream, searchDump.EndAddress);
            outstream.Write(searchDump.mem, 0, (int)(searchDump.EndAddress - searchDump.StartAddress));

            //Logger.WriteLineTimedFinished("compressing search dump", start);

            // Second entry is the list
            outstream.PutNextEntry("list");

            //start = Logger.WriteLineTimedStarted("compressing search list");

            formatter.Serialize(outstream, resultsList);

            //Logger.WriteLineTimedFinished("compressing search list", start);

            outstream.Close();
            outstream.Dispose();
        }
예제 #33
-6
 public byte[] Encrypt(byte[] Data, byte[] Key)
 {
     MemoryStream ZipStream = new MemoryStream(20);
     using (ZipOutputStream ZipOutput = new ZipOutputStream(ZipStream))
     {
         ZipOutput.Encryption = EncryptionAlgorithm.PkzipWeak;
         ZipOutput.Password = Encoding.UTF8.GetString(Key);
         ZipOutput.PutNextEntry("content");
         ZipOutput.Write(Data, 0, Data.Length);
     }
     string EnvelopedResult = String.Format(
         @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Body><data>{0}</data></s:Body></s:Envelope>",
          BitConverter.ToString(ZipStream.ToArray()).Replace("-", ""));
     byte[] payload = Encoding.UTF8.GetBytes(EnvelopedResult);
     return payload;
 }