拜托我不希望用ZIP的,我希望是自己写的一个文本压缩方法!

解决方案 »

  1.   

    如果过你把xml里面的"<","/>",">"这些符合用个特殊的二进制替换例如用1111,我们都知道在文本编码中一个字符是16位的编码,如果用四位的替换不就压缩了.
      

  2.   

    安装RAR的完全版本,里面有个文件Console RAR Manual,用一个piple操作可以解决问题ge
      

  3.   

    至于加密,用.Net提供的一个加密的类,我不记得了。学习一下 ^_^
      

  4.   

    #region
    /// <summary>
    /// This Class is for zip folder
    /// </summary>
    public class Cls_ZIP
    {
    public const string DIR_COMP = @"\compressed";
    public const string DIR_EXP  = @"\expanded"; public string FileZip(string foldername)
    {
    // get application directory
    string currentpath = Application.ExecutablePath.ToString();
    int i = currentpath.IndexOf(@"\bin\");
    if (i > 0) currentpath = currentpath.Substring(0, i); // create directory for compressed files
    if (Directory.Exists(currentpath + DIR_COMP))
    Directory.Delete(currentpath + DIR_COMP, true);
    Directory.CreateDirectory(currentpath + DIR_COMP); // prepare to collect compression statistics
    long count = 0;
    long size = 0;
    long sizeCompressed = 0;
    long ticks = DateTime.Now.Ticks; // compress all files in application dir into compressed dir
    string[] files = Directory.GetFiles(foldername);
    foreach (string srcFile in files)
    {
    // compress file
    string dstFile = currentpath + DIR_COMP + "\\" + Path.GetFileName(srcFile) + ".cmp";
    CompressFile(dstFile, srcFile); // update stats
    count++;
    size += new FileInfo(srcFile).Length;
    sizeCompressed += new FileInfo(dstFile).Length;
    } // show stats
    string msg = string.Format(
    "Compressed {0} files in {1} ms.\r\n" +
    "Original size:   {2:#,###}\r\n" +
    "Compressed size: {3:#,###} ({4:0.00}% of original)",
    count,
    (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond,
    size, sizeCompressed,
    (sizeCompressed / (double)size) * 100.0);
    return msg;
    } public string FileUnzip(string foldername)
    {
    // get application directory
    string path = Application.ExecutablePath;
    int i = path.IndexOf(@"\bin\");
    if (i > 0) path = path.Substring(0, i); // create directory for expanded files
    if (Directory.Exists(path + DIR_EXP))
    Directory.Delete(path + DIR_EXP, true);
    Directory.CreateDirectory(path + DIR_EXP); // prepare to collect compression statistics
    long count = 0;
    long size = 0;
    long sizeExpanded = 0;
    long ticks = DateTime.Now.Ticks; // expand all files in "compressed" dir to "expanded" dir
    string[] files = Directory.GetFiles(path + DIR_COMP);
    foreach (string srcFile in files)
    {
    // expand file
    string dstFile = path + DIR_EXP + "\\" + Path.GetFileName(srcFile);
    dstFile = dstFile.Replace(".cmp", "");
    ExpandFile(dstFile, srcFile); // update stats
    count++;
    size += new FileInfo(srcFile).Length;
    sizeExpanded += new FileInfo(dstFile).Length;
    } // show stats
    string msg = string.Format(
    "Expanded {0} files in {1} ms.\r\n" +
    "Original size:   {2:#,###}\r\n" +
    "Expanded size: {3:#,###} ({4:0.00} x size of compressed)",
    count,
    (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond,
    size, sizeExpanded,
    sizeExpanded / (double)size);
    return msg;
    }
    private static bool CompressFile(string dstFile, string srcFile)
    {
    // prepare to compress file
    bool retval = true;
    FileStream srcStream = null;
    FileStream dstStream = null;
    try
    {
    // open the files
    srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
    dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write); // open compressor stream on destination file
    C1ZStreamWriter sw = new C1ZStreamWriter(dstStream); // copy source into compressor stream
    StreamCopy(sw, srcStream);
    }
    catch // exception? tell caller we failed
    {
    retval = false;
    }
    finally // always close our streams
    {
    if (srcStream != null) srcStream.Close();
    if (dstStream != null) dstStream.Close();
    } // done
    return retval;
    }
    private static bool ExpandFile(string dstFile, string srcFile)
    {
    // prepare to expand file
    bool retval = true;
    FileStream srcStream = null;
    FileStream dstStream = null;
    try
    {
    // open the files
    srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
    dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write); // open expander stream on compressed source
    C1ZStreamReader sr = new C1ZStreamReader(srcStream); // copy expander stream into destination file
    StreamCopy(dstStream, sr);

    catch // exception? tell caller we failed
    {
    retval = false;
    }
    finally // always close our streams
    {
    if (srcStream != null) srcStream.Close();
    if (dstStream != null) dstStream.Close();
    } // done
    return retval;
    }
    private static void StreamCopy(Stream dstStream, Stream srcStream)
    {
    byte[] buffer = new byte[32768];
    int read;
    while ((read = srcStream.Read(buffer, 0, buffer.Length)) != 0)
    dstStream.Write(buffer, 0, read);
    dstStream.Flush();
    }
    }
    #endregion
      

  5.   

    我自己写的一个压缩,需要C1.C1Zip;
    ComponentOne那里都有的,下一个好了!
      

  6.   

    要是能有自己写的压缩XML就好了,不要第三方的DLL,再挂一天,不行就结帖了!
      

  7.   

    http://www-900.ibm.com/developerWorks/cn/xml/x-matters/part19/index_eng.shtml
      

  8.   

    http://www-900.ibm.com/developerWorks/cn/xml/x-tipcomp.shtml
    在IBM DEVELOPERWORKS中有很多关于XML压缩的讨论,去看看吧