Example #1
0
        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 );

            }
        }
Example #2
0
        public static byte[] TryUnCompressByteStream(byte[] compressedBytes)
        {
            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);
                }
            }

            return uncompressedBytes;
        }
Example #3
0
        /// <summary>
        /// Unzips and uncompresses a zip file.
        /// You should give the full path for the zipFileName.
        /// You should give the full path for the rootDirectoryName.
        /// Will unzip and uncompress zipFileName and put the files in the location of rootDirectoryName
        /// </summary>
        /// <param name="zipFileName"></param>
        /// <param name="rootDirectoryName"></param>
        /// <param name="listFilesUnzipped"></param>
        /// <returns></returns>
        public static bool TryUnzipAndUncompressFiles(string zipFileName, string rootDirectoryName, out string[] listFilesUnzipped, out string errorMsg)
        {
            errorMsg = string.Empty;
            listFilesUnzipped = null;
            List<string> listFiles = new List<string>();

            if (!File.Exists(zipFileName))
            {
                errorMsg = string.Format("The given file name, {0}, does not exists.", zipFileName);
                return false;

            }

            if (rootDirectoryName[rootDirectoryName.Length - 1] != Path.DirectorySeparatorChar)
            {
                // append directory separator char
                rootDirectoryName += Path.DirectorySeparatorChar;
            }

            if (!Directory.Exists(rootDirectoryName))
                Directory.CreateDirectory(rootDirectoryName);

            bool ok = false;
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)))
            {
                try
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string fullPath = rootDirectoryName + theEntry.Name;
                        string fileName = fullPath;
                        if (fullPath.Length > 170)
                        {
                            fileName = System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + Guid.NewGuid();
                        }
                        //if the directory doesn't exist, we need to create it
                        string dirName = Path.GetDirectoryName(fileName);
                        if (!Directory.Exists(dirName))
                            Directory.CreateDirectory(dirName);

                        if (File.Exists(fileName))
                            File.Delete(fileName);

                        // create the file
                        using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            long dataSize = theEntry.Size > 0 ? theEntry.Size : 1024;
                            byte[] data = new byte[dataSize];
                            while (true)
                            {
                                int size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                    fs.Write(data, 0, size);
                                else
                                    break;
                            }
                        }

                        if (fullPath != fileName)
                        {
                            //defect 1969 fix
                            //if the fullPath has been manipulated above, the path may not yet exist
                            dirName = Path.GetDirectoryName(fullPath);
                            if (!Directory.Exists(dirName))
                                Directory.CreateDirectory(dirName);

                            //If a file with the same name exists in the target path then we need to delete it before
                            //we move otherwise .NET will throw an exception
                            if (File.Exists(fullPath))
                                File.Delete(fullPath);

                            File.Move(fileName, fullPath);
                        }

                        listFiles.Add(fullPath);
                    }

                    listFilesUnzipped = listFiles.ToArray();

                    ok = true;
                }
                catch (PathTooLongException)
                {

                    errorMsg = string.Format("Can not unzip the given zip file {0}.\r\n" +
                        "A path in the zip file combined with the extraction folder would result in a path that exceeds" +
                        "the Operating System limitation of 248 charaters for a folder path, or 260 characters for a file path.\r\n" +
                        "Extraction Folder: {1}.", zipFileName, rootDirectoryName);

                    try
                    {
                        foreach (string file in listFiles)
                        {
                            File.Delete(file);
                        }
                    }
                    catch { }

                    ok = false;
                }
                catch(Exception)
                {
                    ok = false;
                }
            }
            return ok;
        }