Exemple #1
0
        private void _CompressZIP(string pathFileZip, IDTSComponentEvents componentEvents)
        {
            System.IO.DirectoryInfo di   = new System.IO.DirectoryInfo(_folderSource);
            System.IO.FileInfo[]    fi_s = di.GetFiles("*.*", (_recurse ? System.IO.SearchOption.AllDirectories : System.IO.SearchOption.TopDirectoryOnly));
            bool b = false;

            try
            {
                using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream fz = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(System.IO.File.Create(pathFileZip)))
                {
                    fz.SetLevel(9);

                    if (!string.IsNullOrEmpty(_comment))
                    {
                        componentEvents.FireInformation(1, "UnZip SSIS", "Set Comment.", null, 0, ref b);
                        fz.SetComment(_comment);
                    }

                    if (!string.IsNullOrWhiteSpace(_password))
                    {
                        componentEvents.FireInformation(1, "UnZip SSIS", "Set Password.", null, 0, ref b);
                        fz.Password = _password;
                    }


                    foreach (System.IO.FileInfo fi in fi_s)
                    {
                        if (!System.Text.RegularExpressions.Regex.Match(fi.FullName, _fileFilter).Success)
                        {
                            componentEvents.FireInformation(1, "UnZip SSIS", _typeCompression.ToString() + ": file " + fi.FullName + " doesn't match regex filter '" + _fileFilter + "'. File not processed.", null, 0, ref b);
                            continue;
                        }

                        componentEvents.FireInformation(1, "UnZip SSIS", _typeCompression.ToString() + ": Compress (with '" + _storePaths.ToString() + "') file: " + fi.FullName, null, 0, ref b);

                        string file_name = "";
                        ICSharpCode.SharpZipLib.Zip.ZipEntry ze = null;

                        if (_storePaths == Store_Paths.Absolute_Paths)
                        {
                            //Absolute Path
                            file_name = ICSharpCode.SharpZipLib.Zip.ZipEntry.CleanName(fi.FullName);
                            ze        = new ICSharpCode.SharpZipLib.Zip.ZipEntry(file_name);
                        }
                        else if (_storePaths == Store_Paths.Relative_Paths)
                        {
                            //Relative Path
                            ICSharpCode.SharpZipLib.Zip.ZipNameTransform zn = new ICSharpCode.SharpZipLib.Zip.ZipNameTransform(_folderSource);
                            file_name = zn.TransformFile(fi.FullName);
                            if (_addRootFolder)
                            {
                                file_name = (di.Name + "/" + file_name).Replace("//", "/");
                            }
                            ze = new ICSharpCode.SharpZipLib.Zip.ZipEntry(file_name);
                        }
                        else if (_storePaths == Store_Paths.No_Paths)
                        {
                            //No Path
                            file_name = fi.Name;
                            ze        = new ICSharpCode.SharpZipLib.Zip.ZipEntry(file_name);
                        }
                        else
                        {
                            throw new Exception("Please select type Store Paths (No_Paths / Relative_Paths / Absolute_Paths).");
                        }

                        using (System.IO.FileStream fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                        {
                            ze.Size = fs.Length;
                            fz.PutNextEntry(ze);

                            fs.CopyTo(fz);

                            fs.Flush();
                            fz.Flush();

                            fz.CloseEntry();
                        }
                    }

                    fz.Flush();
                }

                _Check_ZIP(pathFileZip, componentEvents);
            }
            catch (Exception ex)
            {
                componentEvents.FireError(1000, "UnZip SSIS", ex.Message, null, 0);
                throw;
            }
            finally
            {
            }
        }