www.pudn.com > Fetion.rar > GZipCompressXml.cs


namespace Imps.Client.Pc.CustomEmotionUI 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.IO.Compression; 
    using System.Xml; 
 
    public class GZipCompressXml 
    { 
        public static void Compress(IList fileNames, string gzipFile, XmlDocument listDoc) 
        { 
            if (fileNames.Count > 0) 
            { 
                ValidateDirectoryOfFile(gzipFile); 
                Stream stream = null; 
                Stream output = null; 
                try 
                { 
                    output = new FileStream(gzipFile, FileMode.Create, FileAccess.Write); 
                    stream = new MemoryStream(); 
                    XmlWriterSettings settings = new XmlWriterSettings(); 
                    settings.Indent = true; 
                    settings.IndentChars = "\t"; 
                    XmlWriter writer = XmlWriter.Create(output, settings); 
                    writer.WriteStartDocument(); 
                    writer.WriteStartElement("files"); 
                    using (MemoryStream stream3 = new MemoryStream()) 
                    { 
                        listDoc.Save(stream3); 
                        byte[] buffer = new byte[stream3.Length]; 
                        stream3.Seek(0L, SeekOrigin.Begin); 
                        stream3.Read(buffer, 0, buffer.Length); 
                        stream3.Close(); 
                        stream3.Dispose(); 
                        writer.WriteStartElement("file"); 
                        writer.WriteAttributeString("filename", "list.xml"); 
                        writer.WriteString(Convert.ToBase64String(buffer, Base64FormattingOptions.None)); 
                        writer.WriteEndElement(); 
                        writer.Flush(); 
                    } 
                    foreach (string str in fileNames) 
                    { 
                        byte[] inArray = File.ReadAllBytes(str); 
                        if (inArray.Length <= 0x1400000) 
                        { 
                            writer.WriteStartElement("file"); 
                            writer.WriteAttributeString("filename", Path.GetFileName(str)); 
                            writer.WriteString(Convert.ToBase64String(inArray, Base64FormattingOptions.None)); 
                            writer.WriteEndElement(); 
                            writer.Flush(); 
                        } 
                    } 
                    writer.WriteEndElement(); 
                    writer.WriteEndDocument(); 
                    writer.Flush(); 
                    writer.Close(); 
                    StreamClose(ref stream); 
                    StreamClose(ref output); 
                } 
                catch (Exception exception) 
                { 
                    StreamClose(ref stream); 
                    StreamClose(ref output); 
                    try 
                    { 
                        File.Delete(gzipFile); 
                    } 
                    catch 
                    { 
                    } 
                    throw exception; 
                } 
                finally 
                { 
                    GC.Collect(); 
                } 
            } 
        } 
 
        public static void DeCompress(string fileName, string dirPath) 
        { 
            ParseXml2Files(fileName, dirPath); 
        } 
 
        private static void DeCompressGZip(string fileName, string dirPath) 
        { 
            if (File.Exists(fileName)) 
            { 
                ValidateDirectoryOfFile(dirPath); 
                Stream stream = null; 
                Stream stream2 = null; 
                Stream stream3 = null; 
                try 
                { 
                    int num; 
                    stream = File.OpenRead(fileName); 
                    stream2 = new FileStream(dirPath, FileMode.Create, FileAccess.Write); 
                    stream3 = new GZipStream(stream, CompressionMode.Decompress, true); 
                    byte[] buffer = new byte[0x1000]; 
                    while ((num = stream3.Read(buffer, 0, buffer.Length)) != 0) 
                    { 
                        stream2.Write(buffer, 0, num); 
                        stream2.Flush(); 
                    } 
                } 
                catch (Exception exception) 
                { 
                    throw exception; 
                } 
                finally 
                { 
                    StreamClose(ref stream3); 
                    StreamClose(ref stream2); 
                    StreamClose(ref stream); 
                    GC.Collect(); 
                } 
            } 
        } 
 
        private static void ParseXml2Files(string xmlFile, string dirPath) 
        { 
            if (File.Exists(xmlFile)) 
            { 
                ValidateDirectory(dirPath); 
                XmlReaderSettings settings = new XmlReaderSettings(); 
                settings.ConformanceLevel = ConformanceLevel.Fragment; 
                settings.IgnoreWhitespace = true; 
                settings.IgnoreComments = true; 
                XmlReader reader = null; 
                try 
                { 
                    reader = XmlReader.Create(xmlFile, settings); 
                    while (reader.Read()) 
                    { 
                        string name = reader.Name; 
                        if (reader.IsStartElement("file") && !reader.IsEmptyElement) 
                        { 
                            string attribute = reader.GetAttribute("filename"); 
                            if ((attribute != null) && !attribute.Equals(string.Empty)) 
                            { 
                                int num; 
                                reader.Read(); 
                                FileStream stream = new FileStream(Path.Combine(dirPath, attribute), FileMode.Create, FileAccess.Write); 
                                byte[] buffer = new byte[0x1000]; 
                                while ((num = reader.ReadContentAsBase64(buffer, 0, buffer.Length)) != 0) 
                                { 
                                    stream.Write(buffer, 0, num); 
                                    stream.Flush(); 
                                } 
                                stream.Close(); 
                                stream.Dispose(); 
                            } 
                        } 
                    } 
                } 
                catch (Exception exception) 
                { 
                    throw exception; 
                } 
                finally 
                { 
                    if (reader != null) 
                    { 
                        reader.Close(); 
                    } 
                    GC.Collect(); 
                } 
            } 
        } 
 
        private static void StreamClose(ref Stream stream) 
        { 
            if (stream != null) 
            { 
                stream.Close(); 
                stream.Dispose(); 
                stream = null; 
            } 
        } 
 
        private static void StreamWrite(Stream input, Stream output, ref long offset) 
        { 
            int num; 
            input.Seek(offset, SeekOrigin.Begin); 
            byte[] buffer = new byte[0x1000]; 
            while ((num = input.Read(buffer, 0, buffer.Length)) != 0) 
            { 
                output.Write(buffer, 0, num); 
                output.Flush(); 
            } 
            offset = input.Position; 
        } 
 
        private static void ValidateDirectory(string pathName) 
        { 
            if (!Directory.Exists(pathName)) 
            { 
                Directory.CreateDirectory(pathName); 
            } 
        } 
 
        private static void ValidateDirectoryOfFile(string fileName) 
        { 
            FileInfo info = new FileInfo(fileName); 
            if (!info.Directory.Exists) 
            { 
                info.Directory.Create(); 
            } 
        } 
    } 
}