コード例 #1
0
ファイル: TestCompress.cs プロジェクト: plamikcho/xbrlpoc
        public void TestCompressDecompress()
        {
            // get the bytes
            byte[] bytes = ExtractTestFolio123();

            Assert.IsFalse( bytes.Length == 0, "no bytes read or returned" );

            // and compress them
            byte[] compressedBytes = null;
            using ( MemoryStream ms = new MemoryStream() )
            {
                using ( ZipOutputStream zos = new ZipOutputStream( ms ) )
                {
                    zos.SetLevel(9);	// max compression
                    // add an entry
                    zos.PutNextEntry( new ZipEntry( "test" ) );

                    zos.Write( bytes, 0, bytes.Length );
                }

                compressedBytes = ms.ToArray();
            }

            // and decompress

            // and compare the returned bytes against the original bytes
            byte[] uncompressedBytes = null;
            using ( MemoryStream ms = new MemoryStream( compressedBytes ) )
            {
                // decompress the file
                using ( ZipInputStream zis = new ZipInputStream( ms  ) )
                {
                    // should only be on entry per file
                    ZipEntry entry = zis.GetNextEntry();
                    uncompressedBytes = new byte[ entry.Size ];
                    zis.Read( uncompressedBytes, 0, (int)entry.Size );
                }
            }

            // and compare them
            Assert.AreEqual( bytes.Length, uncompressedBytes.Length, "byte lengths are different" );

            for ( int i=0; i < bytes.Length; ++i )
            {
                Assert.AreEqual( bytes[i], uncompressedBytes[i], "bytes differ at position " + i );

            }
        }
コード例 #2
0
ファイル: ZIPUtilities.cs プロジェクト: plamikcho/xbrlpoc
        public static byte[] TryCompressByteStream(byte[] bytes)
        {
            byte[] compressedBytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                using (ZipOutputStream zos = new ZipOutputStream(ms))
                {
                    zos.SetLevel(9);	// max compression
                    // add an entry
                    zos.PutNextEntry(new ZipEntry("test"));

                    zos.Write(bytes, 0, bytes.Length);
                }

                compressedBytes = ms.ToArray();
            }

            return compressedBytes;
        }
コード例 #3
0
ファイル: ZIPUtilities.cs プロジェクト: plamikcho/xbrlpoc
        /// <summary>
        /// Zips and compresses files.
        /// You should give the full path for the zipFileName.
        /// You should give the full path for the rootDirectoryName.
        /// You should give relative path for the names of the files to zip. Relative to the root
        ///		Example: ZipCompressUtility.TryZipAndCompressFiles( "C:\Aucent\Folio1.zip",
        ///						"C:\DirectoryWithHtmlFiles\Folio1",
        ///						new string[]{"index.html", "DependentHoppers\hopper.html"} );
        /// </summary>
        /// <param name="zipFileName">Set to the name that you want the zip file to be. (e.g. C:\Aucent\SalesFolio.zip)</param>
        /// <param name="rootDirectoryName">The root directory to start looking for files and directories in.</param>
        /// <param name="filesToZip">List of all the files you want to zip.</param>
        /// <returns></returns>
        public static bool TryZipAndCompressFiles(string zipFileName, string rootDirectoryName, string[] filesToZip)
        {
            bool ok = false;
            using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
            {
                try
                {
                    if (rootDirectoryName[rootDirectoryName.Length - 1] != Path.DirectorySeparatorChar)
                    {
                        // append directory separator char
                        rootDirectoryName += Path.DirectorySeparatorChar;
                    }

                    s.SetLevel(9); // 0 - store only to 9 - means best compression

                    if (filesToZip != null)
                    {
                        foreach (string file in filesToZip)
                        {
                            byte[] buffer = FileUtilities.ReadFileToByteArray(rootDirectoryName + file);
                            if (buffer != null)
                            {
                                ZipEntry entry = new ZipEntry(file);
                                s.PutNextEntry(entry);
                                s.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }

                    ok = true;
                }
                catch
                {
                    ok = false;
                }
            }
            return ok;
        }