private Stream CompressToZip(ItemNameValueCollection entriesPathId) { var stream = TempStream.Create(); using (var zip = new ionic::Ionic.Zip.ZipOutputStream(stream, true)) { zip.CompressionLevel = ionic::Ionic.Zlib.CompressionLevel.Level3; zip.AlternateEncodingUsage = ionic::Ionic.Zip.ZipOption.AsNecessary; zip.AlternateEncoding = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage); foreach (var path in entriesPathId.AllKeys) { if (Canceled) { zip.Dispose(); stream.Dispose(); return(null); } var counter = 0; foreach (var entryId in entriesPathId[path]) { var newtitle = path; File file = null; var convertToExt = string.Empty; if (!string.IsNullOrEmpty(entryId)) { file = FileDao.GetFile(entryId); if (file.ContentLength > SetupInfo.AvailableFileSize) { Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)); continue; } if (_files.ContainsKey(file.ID.ToString())) { if (_quotaDocsEdition) { 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) { if (file.ConvertedType != null || !string.IsNullOrEmpty(convertToExt)) { //Take from converter try { using (var readStream = !string.IsNullOrEmpty(convertToExt) ? FileConverter.Exec(file, convertToExt) : FileConverter.Exec(file)) { if (readStream != null) { readStream.StreamCopyTo(zip); if (!string.IsNullOrEmpty(convertToExt)) { FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloadedAs, file.Title, convertToExt); } else { FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloaded, file.Title); } } } } catch (Exception ex) { Error = ex.Message; Logger.Error(Error, ex); } } else { using (var readStream = FileDao.GetFileStream(file)) { readStream.StreamCopyTo(zip); FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloaded, file.Title); } } } counter++; } ProgressStep(); } return(stream); } }
private Stream CompressToZip(ItemNameValueCollection entriesPathId) { var stream = TempStream.Create(); using (var zip = new ionic::Ionic.Zip.ZipOutputStream(stream, true)) { zip.CompressionLevel = ionic::Ionic.Zlib.CompressionLevel.Level3; zip.AlternateEncodingUsage = ionic::Ionic.Zip.ZipOption.AsNecessary; zip.AlternateEncoding = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage); foreach (var path in entriesPathId.AllKeys) { if (Canceled) { zip.Dispose(); stream.Dispose(); return null; } var counter = 0; foreach (var entryId in entriesPathId[path]) { var newtitle = path; File file = null; var convertToExt = string.Empty; if (!string.IsNullOrEmpty(entryId)) { FileDao.InvalidateCache(entryId); file = FileDao.GetFile(entryId); if (file.ContentLength > SetupInfo.AvailableFileSize) { Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)); continue; } if (_files.ContainsKey(file.ID.ToString())) { if (_quotaDocsEdition) 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) { if (file.ConvertedType != null || !string.IsNullOrEmpty(convertToExt)) { //Take from converter try { using (var readStream = !string.IsNullOrEmpty(convertToExt) ? FileConverter.Exec(file, convertToExt) : FileConverter.Exec(file)) { if (readStream != null) { readStream.StreamCopyTo(zip); if (!string.IsNullOrEmpty(convertToExt)) { FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloadedAs, file.Title, convertToExt); } else { FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloaded, file.Title); } } } } catch(Exception ex) { Error = ex.Message; Logger.Error(Error, ex); } } else { using (var readStream = FileDao.GetFileStream(file)) { readStream.StreamCopyTo(zip); FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloaded, file.Title); } } } counter++; } ProgressStep(); } return stream; } }