ITPUB论坛 » 开发工具和语言 » .NET软件架构与模式 » .net压缩txt为zip文件,急!
新一届的微软MVP评选已经开始,欢迎各位推荐!
2008-7-1 11:56 liuzhu19
.net压缩txt为zip文件,急!

各位大虾好,本人写了个压缩文件的方法,用的。net自带的 GZipStream类,输入的参数都是要求的,编译通过了,
可是压缩不了文件,下午要初步上线了,急啊!
class Compress
    {
        public void ComPresssMain(string p_strPath)
        {
            try
            {
                string strCotent = FileOperation.ReadFile(p_strPath);
                byte[] bCotent = Encoding.ASCII.GetBytes(strCotent);
                bCotent = ComPressStream(bCotent);

                FileOperation.WriteFile(p_strPath + ".ZIP", bCotent.ToString());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        public byte[] ComPressStream(byte[] p_bCotent)
        {
            MemoryStream ms = new MemoryStream();
            Stream ZipStream = new GZipStream(ms, CompressionMode.Compress,true);
            ZipStream.Write(p_bCotent, 0, p_bCotent.Length);
            ZipStream.Close();
            byte[] bResult = new byte[ms.Length];
            ms.Read(bResult, 0, bResult.Length);
            return bResult;
        }
    }

2008-7-1 13:16 gangyaocn
public static void CreateCompressFile(string sourceName, string destinationName)
        {
            using (Stream source = new FileStream(sourceName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
                {
                    using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
                    {
                        byte[] bytes = new byte[4096];
                        int n;
                        while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            output.Write(bytes, 0, n);
                        }
                    }
                }
            }
        }

2008-7-1 13:16 gangyaocn
for test

2008-7-1 13:21 gangyaocn
还有一个是根据.net sdk自带例子改过来的

2008-7-1 13:22 gangyaocn
public static void CompressFile( string sourceFile, string destinationFile )
        {
            // make sure the source file is there
            if ( File.Exists ( sourceFile ) == false )
                throw new FileNotFoundException ( );

            // Create the streams and byte arrays needed
            byte[] buffer = null;
            FileStream sourceStream = null;
            FileStream destinationStream = null;
            GZipStream compressedStream = null;

            try
            {
                // Read the bytes from the source file into a byte array
                sourceStream = new FileStream ( sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read );

                // Read the source stream values into the buffer
                buffer = new byte[sourceStream.Length];
                int checkCounter = sourceStream.Read ( buffer, 0, buffer.Length );

                if ( checkCounter != buffer.Length )
                {
                    throw new ApplicationException ( );
                }

                // Open the FileStream to write to
                destinationStream = new FileStream ( destinationFile, FileMode.OpenOrCreate, FileAccess.Write );

                // Create a compression stream pointing to the destiantion stream
                compressedStream = new GZipStream ( destinationStream, CompressionMode.Compress, true );

                // Now write the compressed data to the destination file
                compressedStream.Write ( buffer, 0, buffer.Length );
            }
            catch ( ApplicationException ex )
            {
                Console.WriteLine( "压缩文件时发生错误:" + ex.Message);
            }
            finally
            {
                // Make sure we allways close all streams
                if ( sourceStream != null )
                    sourceStream.Close ( );

                if ( compressedStream != null )
                    compressedStream.Close ( );

                if ( destinationStream != null )
                    destinationStream.Close ( );
            }
        }

2008-7-1 14:50 liuzhu19
先谢了
可是我的哪里错了啊,为啥压缩不了

2008-7-1 15:53 liuzhu19
刚才试过了,提过的压缩算法,可以运行,多谢兄台!
可是程序还需要解压,刚才的算法有配套的解压算法么。。。

2008-7-1 17:55 gangyaocn
public static void DecompressFile(string sourceFile, string destinationFile)
      {
        if (!File.Exists( sourceFile)) throw new FileNotFoundException();
      using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
        {
        byte [] quartetBuffer = new byte[4];
        int position = (int)sourceStream.Length - 4;
        sourceStream.Position = position;
        sourceStream.Read(quartetBuffer, 0, 4);
        sourceStream.Position = 0;
        int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
        byte[] buffer = new byte[checkLength + 100];
          using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
          {
            int total = 0;
          for (int offset = 0; ; )
          {
            int bytesRead = decompressedStream.Read(buffer, offset, 100);
            if (bytesRead == 0) break;
            offset += bytesRead;
            total += bytesRead;
          }
              using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
              {
                destinationStream.Write(buffer, 0, total);
                destinationStream.Flush();
              }
        }
      }

2008-7-1 17:55 gangyaocn
解压算法

2008-7-1 18:01 gangyaocn
Another Zip lib for you.
[url]http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx[/url]
CSharpZipLib is free.

2008-7-2 08:55 chenlunpan
滥苹果教我code吗。哈哈

2008-7-2 09:37 gangyaocn
[quote]原帖由 [i]chenlunpan[/i] 于 2008-7-2 08:55 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10826095&ptid=1014562][img]http://www.itpub.net/images/common/back.gif[/img][/url]
滥苹果教我code吗。哈哈 [/quote]
小胖哥,咋啦:torture:

2008-7-2 15:51 chenlunpan
[quote]原帖由 [i]gangyaocn[/i] 于 2008-7-2 09:37 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10826711&ptid=1014562][img]http://www.itpub.net/images/common/back.gif[/img][/url]

小胖哥,咋啦:torture: [/quote]
玩了 你又搞错了:) 胖哥是chichunhua::-):

2008-7-2 16:12 flybyfly
[quote]原帖由 [i]chenlunpan[/i] 于 2008-7-2 15:51 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10831832&ptid=1014562][img]http://www.itpub.net/images/common/back.gif[/img][/url]

玩了 你又搞错了:) 胖哥是chichunhua::-): [/quote]

:sweat:
搞错了

2008-7-3 00:15 yining
SharpZipLib和JDK的标准一样,接口也一样。感觉比.NET自带的好用。

2008-7-3 00:20 gangyaocn
[quote]原帖由 [i]yining[/i] 于 2008-7-3 00:15 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10836192&ptid=1014562][img]http://www.itpub.net/images/common/back.gif[/img][/url]
SharpZipLib和JDK的标准一样,接口也一样。感觉比.NET自带的好用。 [/quote]
据说.NET自带这个GZipStream类只能解压自己压缩的文件,呵呵

页: [1]


Powered by ITPUB论坛