Esempio n. 1
0
        public static byte[] IZip(this byte[] data, int level = 9)
        {
            if (data == null)
            {
                return(null);
            }
            if (data.Length == 0)
            {
                return(new byte[0]);
            }

            level = level.ToClosedInterval(0, 9);

            using (MemoryStream memory = new MemoryStream())
            {
                using (Ionic.Zip.ZipOutputStream stream = new Ionic.Zip.ZipOutputStream(memory))
                {
                    stream.CompressionLevel = (Ionic.Zlib.CompressionLevel)level;
                    stream.PutNextEntry("default");
                    stream.Write(data, 0, data.Length);
                }

                return(memory.ToArray());
            }
        }
Esempio n. 2
0
        public string ComprimirArquivo(string arquivo)
        {
            MemoryStream memoryStream = new MemoryStream();
            string       result;

            using (Ionic.Zip.ZipOutputStream zipOutputStream = new Ionic.Zip.ZipOutputStream(memoryStream))
            {
                byte[]             array    = new byte[4096];
                Ionic.Zip.ZipEntry zipEntry = new Ionic.Zip.ZipEntry();
                zipOutputStream.PutNextEntry(Path.GetFileName(arquivo));
                using (FileStream fileStream = File.OpenRead(arquivo))
                {
                    int num;
                    do
                    {
                        num = fileStream.Read(array, 0, array.Length);
                        zipOutputStream.Write(array, 0, num);
                    }while (num > 0);
                }
                zipOutputStream.Close();
                byte[] array2 = memoryStream.ToArray();
                result = Convert.ToBase64String(array2, 0, array2.Length);
            }
            return(result);
        }
Esempio n. 3
0
        public string Comprimir(string diretorio)
        {
            string[]     files        = Directory.GetFiles(diretorio);
            MemoryStream memoryStream = new MemoryStream();
            string       result;

            using (Ionic.Zip.ZipOutputStream zipOutputStream = new Ionic.Zip.ZipOutputStream(memoryStream))
            {
                byte[]   array  = new byte[4096];
                string[] array2 = files;
                for (int i = 0; i < array2.Length; i++)
                {
                    string             path     = array2[i];
                    Ionic.Zip.ZipEntry zipEntry = new Ionic.Zip.ZipEntry();
                    zipOutputStream.PutNextEntry(Path.GetFileName(path));
                    using (FileStream fileStream = File.OpenRead(path))
                    {
                        int num;
                        do
                        {
                            num = fileStream.Read(array, 0, array.Length);
                            zipOutputStream.Write(array, 0, num);
                        }while (num > 0);
                    }
                }
                zipOutputStream.Close();
                byte[] array3 = memoryStream.ToArray();
                result = Convert.ToBase64String(array3, 0, array3.Length);
            }
            return(result);
        }
Esempio n. 4
0
        public string Comprimir2Base64(object[] arquivos)
        {
            MemoryStream memoryStream = new MemoryStream();

            using (Ionic.Zip.ZipOutputStream zipOutputStream = new Ionic.Zip.ZipOutputStream(memoryStream))
            {
                byte[] array = new byte[4096];
                for (int i = 0; i < arquivos.Length; i++)
                {
                    ArquivoPdf         arquivoPdf    = (ArquivoPdf)arquivos[i];
                    MemoryStream       memoryStream2 = new MemoryStream(arquivoPdf.Dados);
                    Ionic.Zip.ZipEntry zipEntry      = new Ionic.Zip.ZipEntry();
                    zipOutputStream.PutNextEntry(arquivoPdf.Nome);
                    int num;
                    do
                    {
                        num = memoryStream2.Read(array, 0, array.Length);
                        zipOutputStream.Write(array, 0, num);
                    }while (num > 0);
                }
                zipOutputStream.Close();
            }
            byte[] array2 = memoryStream.ToArray();
            return(Convert.ToBase64String(array2, 0, array2.Length));
        }
 public ZipOutputStreamWrapper_DotNetZip(Ionic.Zip.ZipOutputStream wrapped)
 {
     SetWrapped(wrapped, false);
 }
Esempio n. 6
0
        private Stream CompressToZip(ItemNameValueCollection entriesPathId)
        {
            var stream = TempStream.Create();

            using (var zip = new Ionic.Zip.ZipOutputStream(stream, true))
            {
                zip.CompressionLevel       = Ionic.Zlib.CompressionLevel.Level3;
                zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.AsNecessary;
                zip.AlternateEncoding      = Encoding.UTF8;

                foreach (var path in entriesPathId.AllKeys)
                {
                    var counter = 0;
                    foreach (var entryId in entriesPathId[path])
                    {
                        if (CancellationToken.IsCancellationRequested)
                        {
                            zip.Dispose();
                            stream.Dispose();
                            CancellationToken.ThrowIfCancellationRequested();
                        }

                        var newtitle = path;

                        File file         = null;
                        var  convertToExt = string.Empty;

                        if (!string.IsNullOrEmpty(entryId))
                        {
                            FileDao.InvalidateCache(entryId);
                            file = FileDao.GetFile(entryId);

                            if (file == null)
                            {
                                Error = FilesCommonResource.ErrorMassage_FileNotFound;
                                continue;
                            }

                            if (file.ContentLength > SetupInfo.AvailableFileSize)
                            {
                                Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize));
                                continue;
                            }

                            if (files.ContainsKey(file.ID.ToString()))
                            {
                                convertToExt = files[file.ID.ToString()];
                                if (!string.IsNullOrEmpty(convertToExt))
                                {
                                    newtitle = FileUtility.ReplaceFileExtension(path, convertToExt);
                                }
                            }
                        }

                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";

                            if (!string.IsNullOrEmpty(entryId))
                            {
                                newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix;
                            }
                            else
                            {
                                break;
                            }
                        }

                        zip.PutNextEntry(newtitle);

                        if (!string.IsNullOrEmpty(entryId) && file != null)
                        {
                            try
                            {
                                if (FileConverter.EnableConvert(file, convertToExt))
                                {
                                    //Take from converter
                                    using (var readStream = FileConverter.Exec(file, convertToExt))
                                    {
                                        readStream.StreamCopyTo(zip);
                                        if (!string.IsNullOrEmpty(convertToExt))
                                        {
                                            FilesMessageService.Send(file, headers, MessageAction.FileDownloadedAs, file.Title, convertToExt);
                                        }
                                        else
                                        {
                                            FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title);
                                        }
                                    }
                                }
                                else
                                {
                                    using (var readStream = FileDao.GetFileStream(file))
                                    {
                                        readStream.StreamCopyTo(zip);
                                        FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Error = ex.Message;
                                Logger.Error(Error, ex);
                            }
                        }
                        counter++;
                    }

                    ProgressStep();
                }
            }
            return(stream);
        }