Example #1
0
        public static string Transform(string xsl, string xml)
        {
            XslCompiledTransform xslt         = new XslCompiledTransform();
            StringReader         stringReader = new StringReader(xsl);
            XmlReader            xmlReader    = XmlReader.Create(stringReader);

            xslt.Load(xmlReader);
            stringReader = new StringReader(xml);
            xmlReader    = XmlReader.Create(stringReader);
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();

            //xmlWriterSettings.Encoding = Encoding.UTF8;
            xmlWriterSettings.Indent             = true;
            xmlWriterSettings.OmitXmlDeclaration = true;
            Stream    stream    = new MemoryStream();
            XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings);

            xslt.Transform
            (
                xmlReader
                , xmlWriter
            );
            byte[]   buffer = StreamDataHelper.ReadDataToBytes(stream);
            Encoding e      = EncodingHelper.IdentifyEncoding(stream, Encoding.Default);
            int      offset = e.GetPreamble().Length;
            string   s      = e.GetString(buffer, offset, buffer.Length - offset);

            return(s);
        }
Example #2
0
        public void CompressStream()
        {
            //压缩 响应
            Stream stream = CompressHelper.GZipCompress(_workStream);

            byte[] buffer = StreamDataHelper.ReadDataToBytes(stream);
            _originalStream.Write(buffer, 0, buffer.Length);
        }
Example #3
0
 public void DecompressStream()
 {
     //解压 请求
     byte[] bytes = StreamDataHelper.ReadDataToBytes(_originalStream);
     bytes = CompressHelper.GZipDecompress(bytes);
     _workStream.Write(bytes, 0, bytes.Length);
     _workStream.Position = 0;
 }
        public static byte[] GetBytes(this MailMessage mailMessage)
        {
            Assembly assembly        = typeof(SmtpClient).Assembly;
            Type     type            = assembly.GetType("System.Net.Mail.MailWriter");
            var      parametersTypes = new[]
            {
                typeof(Stream)
            };
            object x = null;

            using (Stream stream = new MemoryStream())
            {
                var constructorInfo = type.GetConstructor
                                      (
                    BindingFlags.Instance | BindingFlags.NonPublic
                    , null
                    , parametersTypes
                    , null
                                      );
                var func = DynamicCallMethodExpressionTreeInvokerHelper.CreateNewInstanceConstructorInvokerFunc <object>
                           (
                    type
                    , constructorInfo
                           );
                x = func(new[] { stream });
                var action = DynamicCallMethodExpressionTreeInvokerHelper.CreateMethodCallInvokerAction <MailMessage>
                             (
                    typeof(MailMessage)
                    , () =>
                {
                    var methodInfo = typeof(MailMessage).GetMethod
                                     (
                        "Send"
                        , BindingFlags.NonPublic | BindingFlags.Instance
                        , null
                        , new[] { type, typeof(bool) }
                        , null
                                     );
                    return(methodInfo);
                }
                             );
                action
                (
                    mailMessage
                    , new[]
                {
                    x
                    , true
                }
                );
                byte[] buffer = StreamDataHelper.ReadDataToBytes(stream);
                return(buffer);
            }
        }
        public static Stream GZipDecompress(Stream data)
        {
            byte[]       buffer = StreamDataHelper.ReadDataToBytes(data);
            MemoryStream ms     = new MemoryStream(buffer);
            GZipStream   stream = new GZipStream(ms, CompressionMode.Decompress);

            if (stream.CanSeek)
            {
                stream.Position = 0;
            }
            return(stream);
        }
        public static string DataContractSerializerObjectToXml <T>(T Object, DataContractSerializer serializer)
        {
            MemoryStream ms = new MemoryStream();

            serializer.WriteObject(ms, Object);
            byte[] buffer = StreamDataHelper.ReadDataToBytes(ms);
            string xml    = Encoding.UTF8.GetString(buffer);

            ms.Close();
            ms.Dispose();
            ms = null;
            return(xml);
        }
 public static Encoding IdentifyEncoding
 (
     Stream stream
     , Encoding defaultEncoding
 )
 {
     byte[] data = StreamDataHelper.ReadDataToBytes(stream);
     return(IdentifyEncoding
            (
                data
                , defaultEncoding
            ));
 }
        public static byte[] DeflateDecompress(byte[] data)
        {
            MemoryStream  ms     = new MemoryStream(data);
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress);

            byte[] buffer = StreamDataHelper.ReadDataToBytes(stream);
            ms.Close();
            ms.Dispose();
            ms = null;
            stream.Close();
            stream.Dispose();
            stream = null;
            return(buffer);
        }
        public static byte[] DeflateCompress(byte[] DATA)
        {
            MemoryStream  ms     = new MemoryStream();
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true);

            stream.Write(DATA, 0, DATA.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            byte[] buffer = StreamDataHelper.ReadDataToBytes(ms);
            ms.Close();
            ms.Dispose();
            ms = null;
            return(buffer);
        }
        public static byte[] GZipDecompress(byte[] data)
        {
            //Console.WriteLine("GZipDecompress");
            MemoryStream ms     = new MemoryStream(data);
            GZipStream   stream = new GZipStream(ms, CompressionMode.Decompress);

            byte[] buffer = StreamDataHelper.ReadDataToBytes(stream);
            ms.Close();
            ms.Dispose();
            ms = null;
            stream.Close();
            stream.Dispose();
            stream = null;
            return(buffer);
        }
        public static byte[] GZipCompress(byte[] DATA)
        {
            //Console.WriteLine("GZipCompress");
            MemoryStream ms     = new MemoryStream();
            GZipStream   stream = new GZipStream(ms, CompressionMode.Compress, true);

            stream.Write(DATA, 0, DATA.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            byte[] buffer = StreamDataHelper.ReadDataToBytes(ms);
            ms.Close();
            ms.Dispose();
            ms = null;
            return(buffer);
        }
        public static Stream DeflateCompress(Stream DATA)
        {
            byte[]        buffer = StreamDataHelper.ReadDataToBytes(DATA);
            MemoryStream  ms     = new MemoryStream();
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true);

            stream.Write(buffer, 0, buffer.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            if (ms.CanSeek)
            {
                ms.Position = 0;
            }
            return(ms);
        }
        public static Stream GZipCompress(Stream DATA)
        {
            Console.WriteLine("GZipCompress");
            byte[]       buffer = StreamDataHelper.ReadDataToBytes(DATA);
            MemoryStream ms     = new MemoryStream();
            GZipStream   stream = new GZipStream(ms, CompressionMode.Compress, true);

            stream.Write(buffer, 0, buffer.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            if (ms.CanSeek)
            {
                ms.Position = 0;
            }
            return(ms);
        }
Example #14
0
        public static IEnumerable <string> ShardingFileParts
        (
            string originalFile
            , int singlePartSize             = 1024 * 1024
            , string fileExtensionNameSuffix = "x"
        )

        {
            var directory         = Path.GetDirectoryName(originalFile);
            var fileName          = Path.GetFileNameWithoutExtension(originalFile);
            var fileExtensionName = Path.GetExtension(originalFile);

            fileName = Path.Combine
                       (
                directory
                , fileName
                       );
            var destFileNamePattern = fileName + ".{0}.{1}" + fileExtensionName + fileExtensionNameSuffix;
            var fileInfo            = new FileInfo(originalFile);
            //var list = new List<string>();
            var length = fileInfo.Length;

            if
            (
                length
                >
                singlePartSize
            )
            {
                using
                (
                    var sourceStream = File.OpenRead(originalFile)
                )
                {
                    var parts   = Math.Ceiling((1.0d * length) / singlePartSize);
                    var part    = 0;
                    var buffers = StreamDataHelper
                                  .ReadDataToBuffers
                                  (
                        sourceStream
                        , singlePartSize
                                  );
                    foreach (var buffer in buffers)
                    {
                        part++;
                        var destPartFileName = string
                                               .Format
                                               (
                            destFileNamePattern
                            , parts
                            , part
                                               );
                        //zippedPartFileName += "x";
                        using
                        (
                            var fileStream = new FileStream
                                             (
                                destPartFileName
                                , FileMode.Create
                                , FileAccess.Write
                                , FileShare.None
                                             )
                        )
                        {
                            fileStream.Write(buffer, 0, buffer.Length);
                            fileStream.Close();
                            //list.Add(zippedPartFileName);
                            yield
                            return
                                (destPartFileName);
                        }
                    }
                }
                File.Delete(originalFile);
                //File.Delete(fileName);
            }
            else
            {
                //list.Add(zippedFileName);
                var destPartFileName
                    = string.Format
                      (
                          destFileNamePattern
                          , 1
                          , 1
                      );
                File.Move
                (
                    originalFile
                    , destPartFileName
                );

                yield
                return
                    (destPartFileName);
            }
        }
Example #15
0
        private static IEnumerable <string> MergePartialFilesGroupProcess
        (
            string mergedFileName
            , IOrderedEnumerable <string>
            orderedPartialFilesGroup
            , int parts
        )
        {
            var list = new List <string>();
            int i    = 0;

            foreach (var file in orderedPartialFilesGroup)
            {
                i++;
                var fileName         = Path.GetFileName(file);
                var fileNameSegments = fileName.Split('.');
                var ii   = fileNameSegments.Length - 2;
                int part = int.Parse(fileNameSegments[ii]);
                if (part == i)
                {
                    using
                    (
                        var sourceStream = File.OpenRead(file)
                    )
                    {
                        using
                        (
                            var fileStream
                                =
                                    (
                                        parts > 1
                                                ?
                                        new FileStream
                                        (
                                            mergedFileName
                                            , FileMode.OpenOrCreate
                                            , FileAccess.Write
                                            , FileShare.None
                                        )
                                                :
                                        new FileStream
                                        (
                                            mergedFileName
                                            , FileMode.Create
                                            , FileAccess.Write
                                            , FileShare.None
                                        )
                                    )
                        )
                        {
                            if (parts > 1 && i > 1)
                            {
                                fileStream.Seek(0, SeekOrigin.End);
                            }
                            var buffers = StreamDataHelper
                                          .ReadDataToBuffers
                                          (
                                sourceStream
                                , 64 * 1024                         //* 1024
                                          );
                            foreach (var buffer in buffers)
                            {
                                fileStream.Write(buffer, 0, buffer.Length);
                            }
                            fileStream.Close();
                        }
                        sourceStream.Close();
                        list.Add(file);
                    }
                    if (i == parts)
                    {
                        //ExtractFileProcess(zippedFileName);
                        yield
                        return
                            (mergedFileName);

                        foreach (var x in list)
                        {
                            File.Delete(x);
                        }
                    }
                }
                else
                {
                    if (File.Exists(mergedFileName))
                    {
                        File.Delete(mergedFileName);
                    }
                    break;
                }
            }
            list.Clear();
        }