📄 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<string> fileNames, string gzipFile, XmlDocument listDoc)
{
if (fileNames.get_Count() > 0)
{
ValidateDirectoryOfFile(gzipFile);
Stream stream = null;
Stream stream2 = null;
try
{
stream2 = new FileStream(gzipFile, FileMode.Create, FileAccess.Write);
stream = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.set_Indent(true);
settings.set_IndentChars("\t");
XmlWriter writer = XmlWriter.Create(stream2, settings);
writer.WriteStartDocument();
writer.WriteStartElement("files");
using (MemoryStream outStream = new MemoryStream())
{
listDoc.Save(outStream);
byte[] buffer = new byte[outStream.Length];
outStream.Seek((long) 0, SeekOrigin.Begin);
outStream.Read(buffer, 0, buffer.Length);
outStream.Close();
outStream.Dispose();
writer.WriteStartElement("file");
writer.WriteAttributeString("filename", "list.xml");
writer.WriteString(Convert.ToBase64String(buffer, 0));
writer.WriteEndElement();
writer.Flush();
}
using (IEnumerator<string> enumerator = fileNames.GetEnumerator())
{
while (enumerator.MoveNext())
{
string path = enumerator.get_Current();
byte[] buffer2 = File.ReadAllBytes(path);
if (buffer2.Length <= 0x1400000)
{
writer.WriteStartElement("file");
writer.WriteAttributeString("filename", Path.GetFileName(path));
writer.WriteString(Convert.ToBase64String(buffer2, 0));
writer.WriteEndElement();
writer.Flush();
}
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
StreamClose(ref stream);
StreamClose(ref stream2);
}
catch (Exception exception)
{
StreamClose(ref stream);
StreamClose(ref stream2);
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 count;
stream = File.OpenRead(fileName);
stream2 = new FileStream(dirPath, FileMode.Create, FileAccess.Write);
stream3 = new GZipStream(stream, 0, true);
byte[] buffer = new byte[0x1000];
while ((count = stream3.Read(buffer, 0, buffer.Length)) != 0)
{
stream2.Write(buffer, 0, count);
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.set_ConformanceLevel(1);
settings.set_IgnoreWhitespace(true);
settings.set_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 count;
reader.Read();
FileStream stream = new FileStream(Path.Combine(dirPath, attribute), FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[0x1000];
while ((count = reader.ReadContentAsBase64(buffer, 0, buffer.Length)) != 0)
{
stream.Write(buffer, 0, count);
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 count;
input.Seek(offset, SeekOrigin.Begin);
byte[] buffer = new byte[0x1000];
while ((count = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, count);
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();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -