Beispiel #1
0
        public static void Compress(Stream data, Stream outData, string fileName)
        {
            string str = "";

            try
            {
                using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(outData))
                {
                    zipStream.SetLevel(3);
                    ICSharpCode.SharpZipLib.Zip.ZipEntry newEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
                    newEntry.DateTime = DateTime.UtcNow;
                    zipStream.PutNextEntry(newEntry);
                    data.Position = 0;
                    int    size   = (data.CanSeek) ? Math.Min((int)(data.Length - data.Position), 0x2000) : 0x2000;
                    byte[] buffer = new byte[size];
                    int    n;
                    do
                    {
                        n = data.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, n);
                    } while (n != 0);
                    zipStream.CloseEntry();
                    zipStream.Flush();
                    zipStream.Close();
                }
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Constrói um pacote com as dependencias informadas.
        /// </summary>
        /// <param name="outStream">Stream de saída.</param>
        /// <param name="entries"></param>
        /// <returns></returns>
        public static void BuildPackage(System.IO.Stream outStream, IEnumerator <Tuple <DataEntryVersion, System.IO.Stream> > entries)
        {
            outStream.Require("outStream").NotNull();
            outStream.Require("entries").NotNull();
            var buffer = new byte[1024];
            int read   = 0;
            var zipOut = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(outStream);

            while (entries.MoveNext())
            {
                var item = entries.Current;
                if (item == null)
                {
                    continue;
                }
                var entryName = item.Item1.TypeName.FullName;
                var entry     = new ICSharpCode.SharpZipLib.Zip.ZipEntry(entryName);
                entry.Size = item.Item2.Length;
                zipOut.PutNextEntry(entry);
                while ((read = item.Item2.Read(buffer, 0, buffer.Length)) > 0)
                {
                    zipOut.Write(buffer, 0, read);
                }
                zipOut.CloseEntry();
            }
            zipOut.Finish();
            zipOut.Flush();
        }
Beispiel #3
0
        public static MemoryStream CompressStream(MemoryStream stream, string entryname)
        {
            MemoryStream ms = new MemoryStream();

            using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipstream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ms))
            {
                ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(entryname);
                entry.DateTime = DateTime.Now;
                zipstream.PutNextEntry(entry);
                zipstream.Write(stream.ToArray(), 0, (int)stream.Length);
                zipstream.Flush();
                zipstream.Finish();
                zipstream.Close();
            }
            return(ms);
        }
Beispiel #4
0
        private void Compress(Stream data, Stream outData)
        {
            string str = "";

            try
            {
                using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(outData))
                {
                    zipStream.SetLevel(3);
                    ICSharpCode.SharpZipLib.Zip.ZipEntry newEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry("data.xml");
                    newEntry.DateTime = DateTime.UtcNow;
                    //newEntry.Size = data.Length;
                    zipStream.PutNextEntry(newEntry);

                    //data.CopyTo(zipStream);
                    //CopyStream(data, zipStream);
                    //zipStream.Write(data, 0, data.Length);
                    // zipStream.Finish();
                    //zipStream.Close();//?

                    //byte[] buffer = new byte[32768];
                    //int read;
                    //while ((read = data.Read(buffer, 0, buffer.Length)) > 0)
                    //{
                    //    zipStream.Write(buffer, 0, read);
                    //}

                    data.Position = 0;
                    int    size   = (data.CanSeek) ? Math.Min((int)(data.Length - data.Position), 0x2000) : 0x2000;
                    byte[] buffer = new byte[size];
                    int    n;
                    do
                    {
                        n = data.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, n);
                    } while (n != 0);
                    zipStream.CloseEntry();
                    zipStream.Flush();
                    zipStream.Close();
                }
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
        }
Beispiel #5
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
            {
            }
        }
Beispiel #6
0
        private static void Zip(string[] files, ICSharpCode.SharpZipLib.Zip.ZipOutputStream s)
        {
            List <string> rootPaths = new List <string>();

            ICSharpCode.SharpZipLib.Zip.ZipEntry entry = null;
            System.IO.FileStream fs = null;
            ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
            try
            {
                ////创建当前文件夹
                //entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry("/");  //加上 “/” 才会当成是文件夹创建
                //s.PutNextEntry(entry);
                //s.Flush();
                foreach (string file in files)
                {
                    if (System.IO.Directory.Exists(file))
                    {
                        if (file.Split('\\').Count() > 1)
                        {
                            rootPaths.Add(file);
                            entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(file.Split('\\').Last() + "/");  //加上 “/” 才会当成是文件夹创建
                            s.PutNextEntry(entry);
                            s.Flush();
                        }
                        continue;
                    }

                    //打开压缩文件
                    fs = System.IO.File.OpenRead(file);

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    var zipfilename = System.IO.Path.GetFileName(file);
                    foreach (var rootPath in rootPaths)
                    {
                        var _index = file.IndexOf(rootPath);
                        if (_index >= 0)
                        {
                            zipfilename = rootPath.Split('\\').Last() + "\\" + System.IO.Path.GetFileName(file);
                            break;
                        }
                    }
                    entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(zipfilename)
                    {
                        DateTime = DateTime.Now,
                        Size     = fs.Length
                    };
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (entry != null)
                {
                    entry = null;
                }
                GC.Collect();
            }
        }