Exemplo n.º 1
0
        public static void ZipFiles(string inputFolderPath, string outputPathAndFile)
        {
            var ar         = GenerateFileList(inputFolderPath); // generate file list
            var TrimLength = Directory.GetParent(inputFolderPath).ToString().Length;

            // find number of chars to remove     // from orginal file path
            TrimLength += 1; //remove '\'
            FileStream ostream;

            byte[] obuffer;
            var    oZipStream = new ZipOutputStream(File.Create(outputPathAndFile)); // create zip stream

            oZipStream.SetLevel(9);                                                  // maximum compression
            ZipEntry oZipEntry;

            foreach (string Fil in ar) // for each file, generate a zipentry
            {
                oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);

                if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                {
                    ostream = File.OpenRead(Fil);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 多个文件压缩为一个文件 *.zip
        /// </summary>
        /// <param name="files"></param>
        /// <param name="outputFullPathAndFile"></param>
        /// <param name="password"></param>
        public static void ZipFilesList(List <string> files, string outputFullPathAndFile, string password)
        {
            FileStream ostream;

            byte[]          obuffer;
            string          outPath    = outputFullPathAndFile;
            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream

            if (password != null && password != String.Empty)
            {
                oZipStream.Password = password;
            }
            oZipStream.SetLevel(9); // maximum compression
            ZipEntry oZipEntry;

            foreach (string Fil in files) // for each file, generate a zipentry
            {
                ///将文件名称解码为字符串
                oZipEntry = new ZipEntry(HttpUtility.UrlDecode(X2File.GetFileName(Fil)));
                oZipStream.PutNextEntry(oZipEntry);
                if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                {
                    ostream = File.OpenRead(Fil);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                    ostream.Close();
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }
Exemplo n.º 3
0
        public void ZipFiles(string inputFolderPath, string outputPathAndFile)
        {
            ArrayList fileLst    = GenerateFileList(inputFolderPath); // generate file list
            int       TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

            TrimLength += 1;
            FileStream ostream;

            byte[]          obuffer;
            string          outPath    = outputPathAndFile;
            ZipOutputStream oZipStream = new ZipOutputStream(System.IO.File.Create(outPath)); // create zip stream

            oZipStream.SetLevel(9);                                                           // maximum compression
            ZipEntry oZipEntry;
            int      nCurProgress = 0;
            int      nTotalCnt    = fileLst.Count;

            foreach (string Fil in fileLst) // for each file, generate a zipentry
            {
                oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);
                if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                {
                    ostream = System.IO.File.OpenRead(Fil);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                }
                nCurProgress++;
                prgbMemo.Value = nCurProgress * 100 / nTotalCnt;
            }
            oZipStream.Finish();
            oZipStream.Close();
            prgbMemo.Value = 0;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Zips the package.
        /// </summary>
        /// <param name="Path">The path.</param>
        /// <param name="savePath">The save path.</param>
        public static void ZipPackage(string Path, string savePath)
        {
            string OutPath = savePath;

            ArrayList ar = GenerateFileList(Path);
            // generate file list
            // find number of chars to remove from orginal file path
            int TrimLength = (Directory.GetParent(Path)).ToString().Length;

            TrimLength += 1;

            //remove '\'
            FileStream ostream;

            byte[] obuffer;

            ZipOutputStream oZipStream = new ZipOutputStream(System.IO.File.Create(OutPath));

            // create zip stream


            oZipStream.SetLevel(9);
            // 9 = maximum compression level
            ZipEntry oZipEntry;

            foreach (string Fil in ar) // for each file, generate a zipentry
            {
                oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);


                if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                {
                    ostream = File.OpenRead(Fil);

                    obuffer = new byte[ostream.Length];

                    // byte buffer
                    ostream.Read(obuffer, 0, obuffer.Length);

                    oZipStream.Write(obuffer, 0, obuffer.Length);
                    ostream.Close();
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
            oZipStream.Dispose();
            oZipStream = null;

            oZipEntry = null;


            ostream = null;
            ar.Clear();
            ar = null;
        }
Exemplo n.º 5
0
        public void J_Zip(string Path, string FolderName, string outputPathAndFile, string password)
        {
            FileStream ostream;

            byte[]          obuffer;
            ZipEntry        oZipEntry;
            ArrayList       arrList;
            ZipOutputStream oZipStream;

            int    TrimLength = 0;
            string outPath    = "";

            // generate file list
            arrList    = J_GenerateFileList(Path + "\\" + FolderName);
            TrimLength = (Directory.GetParent(Path + "\\" + FolderName)).ToString().Length + 1;
            outPath    = Path + @"\" + outputPathAndFile;

            // create zip stream
            oZipStream = new ZipOutputStream(File.Create(outPath));
            if (password != "" && password != null && password != String.Empty)
            {
                oZipStream.Password = password;
            }

            // maximum compression
            oZipStream.SetLevel(9);

            // for each file, generate a zipentry
            foreach (string Fil in arrList)
            {
                oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);

                // if a file ends with '/' its a directory
                if (!Fil.EndsWith(@"/"))
                {
                    ostream = File.OpenRead(Fil);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);

                    ostream.Close();
                }
            }

            oZipStream.Finish();
            oZipStream.Close();
        }
Exemplo n.º 6
0
        /// <summary>
        /// 文件压缩
        /// </summary>
        /// <param name="inputFolderPath"></param>
        /// <param name="outputPathAndFile"></param>
        /// <param name="password"></param>
        public static void ZipElementFiles(string inputFolderPath, string outputPathAndFile, string password)
        {
            ArrayList ar         = GenerateFileList(inputFolderPath); // generate file list
            int       TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

            // find number of chars to remove     // from orginal file path
            TrimLength += 1; //remove '\'
            FileStream ostream;

            byte[]          obuffer;
            string          outPath    = outputPathAndFile;
            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream

            try
            {
                if (password != null && password != String.Empty)
                {
                    oZipStream.Password = password;
                }
                oZipStream.SetLevel(9);    // maximum compression
                ZipEntry oZipEntry;
                foreach (string Fil in ar) // for each file, generate a zipentry
                {
                    oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                    oZipStream.PutNextEntry(oZipEntry);

                    if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                    {
                        ostream = File.OpenRead(Fil);
                        obuffer = new byte[ostream.Length];
                        ostream.Read(obuffer, 0, obuffer.Length);
                        oZipStream.Write(obuffer, 0, obuffer.Length);
                        ostream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                X2Error.Log("ZipElementFiles", ex, "ZipElementFiles");
                throw ex;
            }
            finally
            {
                oZipStream.Finish();
                oZipStream.Close();
            }
        }
Exemplo n.º 7
0
        public static void ZipFile(string pFile, string outPathAndZipFile, string password)
        {
            string    OutPath = outPathAndZipFile;
            ArrayList ar      = new ArrayList();

            ar.Add(pFile);
            // find number of chars to remove from orginal file path
            string tParent    = (Directory.GetParent(pFile)).ToString();
            int    TrimLength = tParent.Length;

            if (tParent.Substring(tParent.Length - 1, 1) != "\\")
            {
                TrimLength += 1; //remove '\'
            }
            FileStream ostream;

            byte[]          obuffer;
            ZipOutputStream oZipStream = new ZipOutputStream(System.IO.File.Create(OutPath)); // create zip stream

            if (password != String.Empty)
            {
                oZipStream.Password = password;
            }
            oZipStream.SetLevel(9); // 9 = maximum compression level
            ZipEntry oZipEntry;

            foreach (string Fil in ar) // for each file, generate a zipentry
            {
                oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);

                if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                {
                    ostream = File.OpenRead(Fil);
                    obuffer = new byte[ostream.Length]; // byte buffer
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                    Console.Write(".");
                    ostream.Close();
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }
Exemplo n.º 8
0
        // this code snippet was based on the code from http://www.eggheadcafe.com/articles/20050821.asp
        public static void zipFolder(string strPathOfFolderToZip, string strTargetZipFileName)
        {
            //			public static void ZipIt( string Path, string outPathAndZipFile, string password)
            //			{
            ArrayList ar = utils.files.returnPathToAllFilesInFolder_Recursively(strPathOfFolderToZip);
            //ArrayList ar = GenerateFileList(Path);
            // generate file list
            // find number of chars to remove from orginal file path
            int TrimLength = (Directory.GetParent(strPathOfFolderToZip)).ToString().Length;

            TrimLength += 1;             //remove '\'
            FileStream ostream;

            byte[]          obuffer;
            ZipOutputStream oZipStream = new ZipOutputStream(System.IO.File.Create(strTargetZipFileName)); // create zip stream

            oZipStream.SetLevel(9);                                                                        // 9 = maximum compression level

            ZipEntry oZipEntry;

            foreach (string Fil in ar)            // for each file, generate a zipentry
            {
                oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);
                if (!Fil.EndsWith(@"/"))                  // if a file ends with '/' its a directory
                {
                    ostream = File.OpenRead(Fil);
                    obuffer = new byte[ostream.Length];
                    // byte buffer
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                    Console.Write(".");
                    ostream.Close();
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }
Exemplo n.º 9
0
        public static void ZipFiles(string inputFolderPath, string outputFilename, string password)
        {
            var liste      = GenerateFileList(inputFolderPath); // generate file list
            int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

            // find number of chars to remove     // from orginal file path
            TrimLength += 1; //remove '\'
            FileStream ostream;

            byte[] obuffer;
            var    outPath    = Path.Combine(inputFolderPath, outputFilename);
            var    oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream

            using (oZipStream)
            {
                if (!string.IsNullOrEmpty(password))
                {
                    oZipStream.Password = password;
                }
                oZipStream.SetLevel(9);    // maximum compression
                ZipEntry oZipEntry;
                foreach (var Fil in liste) // for each file, generate a zipentry
                {
                    oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                    oZipStream.PutNextEntry(oZipEntry);

                    if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                    {
                        ostream = File.OpenRead(Fil);
                        obuffer = new byte[ostream.Length];
                        ostream.Read(obuffer, 0, obuffer.Length);
                        oZipStream.Write(obuffer, 0, obuffer.Length);
                    }
                }
                //oZipStream.Finish();
                //oZipStream.Close();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// 특정 폴더를 ZIP으로 압축
        /// </summary>
        /// <param name="targetFolderPath">압축 대상 폴더 경로</param>
        /// <param name="zipFilePath">저장할 ZIP 파일 경로</param>
        /// <param name="password">압축 암호</param>
        /// <param name="isDeleteFolder">폴더 삭제 여부</param>
        /// <returns>압축 성공 여부</returns>
        public static bool ZipFiles(string targetFolderPath, string zipFilePath,
                                    string password, bool isDeleteFolder)
        {
            bool retVal = false;

            // 폴더가 존재하는 경우에만 수행.
            if (Directory.Exists(targetFolderPath))
            {
                // 압축 대상 폴더의 파일 목록.
                ArrayList ar = GenerateFileList(targetFolderPath);

                // 압축 대상 폴더 경로의 길이 + 1
                int TrimLength =
                    (Directory.GetParent(targetFolderPath)).ToString().Length + 1;
                // find number of chars to remove. from orginal file path. remove '\'

                FileStream ostream;
                byte[]     obuffer;
                string     outPath = zipFilePath;

                // ZIP 스트림 생성.
                ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath));
                try
                {
                    // 패스워드가 있는 경우 패스워드 지정.
                    if (password != null && password != String.Empty)
                    {
                        oZipStream.Password = password;
                    }

                    oZipStream.SetLevel(9); // 암호화 레벨.(최대 압축)

                    ZipEntry oZipEntry;
                    foreach (string Fil in ar)
                    {
                        oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
                        oZipStream.PutNextEntry(oZipEntry);

                        // 파일인 경우.
                        if (!Fil.EndsWith(@"/"))
                        {
                            ostream = File.OpenRead(Fil);
                            obuffer = new byte[ostream.Length];
                            ostream.Read(obuffer, 0, obuffer.Length);
                            oZipStream.Write(obuffer, 0, obuffer.Length);
                        }
                    }

                    retVal = true;
                }
                catch
                {
                    retVal = false;
                    // 오류가 난 경우 생성 했던 파일을 삭제.
                    if (File.Exists(outPath))
                    {
                        File.Delete(outPath);
                    }
                }
                finally
                {
                    // 압축 종료.
                    oZipStream.Finish();
                    oZipStream.Close();
                }


                // 폴더 삭제를 원할 경우 폴더 삭제.
                if (isDeleteFolder)
                {
                    try
                    {
                        Directory.Delete(targetFolderPath, true);
                    }
                    catch { }
                }
            }
            return(retVal);
        }
Exemplo n.º 11
0
        /// <summary>
        /// This method generate the package as per the <c>PackageSize</c> declare.
        /// Currently <c>PackageSize</c> is 100MB.
        /// </summary>
        /// <param name="inputFolderPath">Input folder Path.</param>
        /// <param name="outputFolderandFile">Output folder Path.</param>
        public static void ZipFiles(string inputFolderPath, string outputFolderandFile)
        {
            ArrayList ar         = GenerateFileList(inputFolderPath); // generate file list
            int       trimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

            // find number of chars to remove   // from original file path
            trimLength += 1;     //remove '\'

            // Output file stream of package.
            FileStream ostream;

            byte[] obuffer;

            // Output Zip file name.
            string outPath = Path.Combine(outputFolderandFile, DateTime.Now.Ticks + ".zip");

            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath), true);     // create zip stream

            // Compression level of zip file.
            oZipStream.CompressionLevel = CompressionLevel.Default;

            // Initialize the zip entry object.
            ZipEntry oZipEntry = new ZipEntry();

            // numbers of files in file list.
            var counter = ar.Count;

            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    Array.Sort(ar.ToArray());            // Sort the file list array.
                    foreach (string Fil in ar.ToArray()) // for each file, generate a zip entry
                    {
                        if (!Fil.EndsWith(@"/"))         // if a file ends with '/' its a directory
                        {
                            if (!zip.ContainsEntry(Path.GetFullPath(Fil.ToString())))
                            {
                                oZipEntry = zip.AddEntry(Path.GetFullPath(Fil.ToString()), Fil.Remove(0, trimLength));
                                counter--;
                                try
                                {
                                    if (counter > 0)
                                    {
                                        if (oZipStream.Position < m_packageSize)
                                        {
                                            oZipStream.PutNextEntry(oZipEntry.FileName);
                                            ostream = File.OpenRead(Fil);
                                            obuffer = new byte[ostream.Length];
                                            ostream.Read(obuffer, 0, obuffer.Length);
                                            oZipStream.Write(obuffer, 0, obuffer.Length);
                                        }

                                        if (oZipStream.Position > m_packageSize)
                                        {
                                            zip.RemoveEntry(oZipEntry);
                                            oZipStream.Flush();
                                            oZipStream.Close();                                                          // close the zip stream.
                                            outPath    = Path.Combine(outputFolderandFile, DateTime.Now.Ticks + ".zip"); // create new output zip file when package size.
                                            oZipStream = new ZipOutputStream(File.Create(outPath), true);                // create zip stream
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("No more file existed in directory");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    zip.RemoveEntry(oZipEntry.FileName);
                                }
                            }
                            else
                            {
                                Console.WriteLine("File Existed {0} in Zip {1}", Path.GetFullPath(Fil.ToString()), outPath);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                oZipStream.Flush();
                oZipStream.Close();    // close stream
                Console.WriteLine("Remain Files{0}", counter);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// This method generate the package as per the <c>PackageSize</c> declare.
        /// Currently <c>PackageSize</c> is 2 GB.
        /// </summary>
        /// <param name="inputFolderPath">Input folder Path.</param>
        /// <param name="outputFolderandFile">Output folder Path.</param>

        static void SplitZipFolder(string inputFolderPath, string outputFolderandFile, string threadId)
        {
            #region otherApproach

            #endregion

            int cnt = 1;
            m_packageSize = m_packageSize * 20;
            ArrayList htmlFile = new ArrayList();
            ArrayList ar       = GenerateFileList(inputFolderPath, out htmlFile); // generate file list



            // Output Zip file name.
            string outPath          = Path.Combine(outputFolderandFile, threadId + "-" + cnt + ".zip");
            long   HTMLstreamlenght = 0;

            using (ZipOutputStream oZipStream = CreateNewStream(htmlFile, outPath, out HTMLstreamlenght))
            {
                // Initialize the zip entry object.
                ZipEntry oZipEntry = new ZipEntry();

                // numbers of files in file list.
                var counter = ar.Count;
                try
                {
                    using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                    {
                        Array.Sort(ar.ToArray());            // Sort the file list array.
                        foreach (string Fil in ar.ToArray()) // for each file, generate a zip entry
                        {
                            if (!Fil.EndsWith(@"/"))         // if a file ends with '/' its a directory
                            {
                                if (!zip.ContainsEntry(Path.GetFileName(Fil.ToString())))
                                {
                                    oZipEntry = zip.AddEntry("Attachments/" + Path.GetFileName(Fil.ToString()), Fil);
                                    counter--;
                                    try
                                    {
                                        if (counter >= 0)
                                        {
                                            long      streamlenght = HTMLstreamlenght;
                                            const int BufferSize   = 4096;
                                            byte[]    obuffer      = new byte[BufferSize];
                                            if (oZipStream.Position + streamlenght < m_packageSize)
                                            {
                                                using (FileStream ostream = File.OpenRead(Fil))
                                                {
                                                    //ostream = File.OpenRead(Fil);
                                                    using (ZipOutputStream tempZStream = new ZipOutputStream(File.Create(Directory.GetCurrentDirectory() + "\\test.zip"), false))
                                                    {
                                                        tempZStream.PutNextEntry(oZipEntry.FileName);
                                                        int tempread;
                                                        while ((tempread = ostream.Read(obuffer, 0, obuffer.Length)) > 0)
                                                        {
                                                            tempZStream.Write(obuffer, 0, tempread);
                                                        }
                                                        streamlenght = tempZStream.Position;
                                                        tempZStream.Dispose();
                                                        tempZStream.Flush();
                                                        tempZStream.Close(); // close the zip stream.
                                                                             // ostream.Dispose();
                                                    }

                                                    File.Delete(Directory.GetCurrentDirectory() + "\\test.zip");
                                                }
                                            }

                                            if (oZipStream.Position + streamlenght > m_packageSize)
                                            {
                                                zip.RemoveEntry(oZipEntry);
                                                zip.Dispose();
                                                oZipStream.Dispose();
                                                oZipStream.Flush();
                                                oZipStream.Close();                                                                    // close the zip stream.
                                                cnt     = cnt + 1;
                                                outPath = Path.Combine(outputFolderandFile, threadId + "-" + cnt.ToString() + ".zip"); // create new output zip file when package size.
                                                using (ZipOutputStream oZipStream2 = CreateNewStream(htmlFile, outPath, out HTMLstreamlenght))
                                                {
                                                    oZipStream2.PutNextEntry(oZipEntry.FileName);
                                                    using (FileStream ostream = File.OpenRead(Fil))
                                                    {
                                                        int read;
                                                        while ((read = ostream.Read(obuffer, 0, obuffer.Length)) > 0)
                                                        {
                                                            oZipStream2.Write(obuffer, 0, read);
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                oZipStream.PutNextEntry(oZipEntry.FileName);
                                                using (FileStream ostream = File.OpenRead(Fil))
                                                {
                                                    int read;
                                                    while ((read = ostream.Read(obuffer, 0, obuffer.Length)) > 0)
                                                    {
                                                        oZipStream.Write(obuffer, 0, read);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine("No more file existed in directory");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex.ToString());
                                        zip.RemoveEntry(oZipEntry.FileName);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("File Existed {0} in Zip {1}", Path.GetFullPath(Fil.ToString()), outPath);
                                }
                            }
                        }
                        zip.Dispose();
                    }
                    oZipStream.Dispose();
                    oZipStream.Flush();
                    oZipStream.Close();// close stream
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    oZipStream.Dispose();
                    oZipStream.Flush();
                    oZipStream.Close();// close stream

                    Console.WriteLine("Remain Files{0}", counter);
                }
            }
        }