适用于 Windows Mobile 的 gzip 工具比 SharpZipLib 更好?

发布于 2024-08-21 08:25:33 字数 1077 浏览 6 评论 0原文

我在用 c# 编写的 Windows Mobile 应用程序上使用 SharpZipLib 进行 gzip

我有这个代码

public static bool gzDecompressFile(String inputFilePath, String outputFilePath)
        {
            if (!File.Exists(inputFilePath))
                return false;

            if (File.Exists(outputFilePath))
                File.Delete(outputFilePath);


            FileStream fs = File.OpenRead(inputFilePath);
            FileStream fsOut = File.OpenWrite(outputFilePath);

            GZipInputStream gzipIn = new GZipInputStream(fs);

            // Compress file in 1kb chunks
            byte[] chunk = new byte[1024];
            int read = 1;

            while (read > 0)
            {
                read = gzipIn.Read(chunk, 0, chunk.Length);

                if (read > 0)
                {
                    fsOut.Write(chunk, 0, read);
                }
            }

            fsOut.Close();
            fs.Close();

            return true;
        }

For a 1.6MB --> 7MB解压需要4-5分钟。
您知道更好的紧凑框架吗?

I use SharpZipLib for gzip on a Windows Mobile application written in c#

I have this code

public static bool gzDecompressFile(String inputFilePath, String outputFilePath)
        {
            if (!File.Exists(inputFilePath))
                return false;

            if (File.Exists(outputFilePath))
                File.Delete(outputFilePath);


            FileStream fs = File.OpenRead(inputFilePath);
            FileStream fsOut = File.OpenWrite(outputFilePath);

            GZipInputStream gzipIn = new GZipInputStream(fs);

            // Compress file in 1kb chunks
            byte[] chunk = new byte[1024];
            int read = 1;

            while (read > 0)
            {
                read = gzipIn.Read(chunk, 0, chunk.Length);

                if (read > 0)
                {
                    fsOut.Write(chunk, 0, read);
                }
            }

            fsOut.Close();
            fs.Close();

            return true;
        }

For a 1.6MB --> 7MB decompression it takes 4-5 minutes.
Do you know a better one for Compact Framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2024-08-28 08:25:33

将 1.6MB 解压缩到 7MB 需要 4 分钟多的时间,速度非常慢。

我想到的一些事项:

  1. 您是否尝试过更改工作缓冲区的大小?恕我直言,1KB 是非常保守的。您可能会遇到 GZip 解压缩算法的性能问题(但这只是 WAG)。
  2. 是否存储了临时文件,如果有,存储在哪里?它们是在内存中还是在存储设备上?如果在存储设备上,则它们可能具有不同的性能特征。
  3. 您可以通过双缓冲来提高性能。这是通过创建两个缓冲区来实现的,当一个缓冲区被读取填充时,另一个缓冲区被写入刷新(乒乓效应,需要异步代码)。它只会与最慢的操作(解压缩流读取)一样快,但您也许能够消除累积的写入延迟。

我们最初使用 SharpLibZip,但后来改用商业 Xceed ZIP.NET 库以利用批处理功能(以及对 .NET 和 .NET Compact Framework 的支持)。 Xceed 库还支持 GZip。即使在我们老式的 400 MHz Windows CE 4.2 设备上,我们也只需要大约 20-30 秒即可解压 6 MB 文件(尽管是使用 XCeed ZIP.NET 的 ZIP 格式)。

另外,如果我没记错的话,我相信 .NET Compact Framework 支持 GZip。另外,将 FileStream 放置在 using 语句中,以确保在方法中间发生异常时关闭它们。

Decompressing 1.6MB to 7MB in over 4 minutes is dreadfully slow.

Some items that come to mind:

  1. Have you tried changing the size of your working buffer? 1KB is very conservative IMHO. You may be running into a performance issue with the GZip decompression algorithm (but that's merely a WAG).
  2. Are there temporary files being stored, and if so, where? Are they in memory or on a storage device? And if on a storage device, which one as they may have different performance characteristics.
  3. You could improve your performance by double-buffering. This works by creating two buffers and while one is being filled from a read the other is being flushed by the write (ping-pong effect, requires asynchronous code). It will only be as fast as the slowest operation (the decompressed stream read) but you may be able to all but eliminate the accumulated write delay.

We originally started with SharpLibZip but switched to the commercial Xceed ZIP.NET library to take advantage of the batching capabilities (and the support for both .NET and .NET Compact Frameworks). The Xceed library also supports GZip. Even on our archaic 400 MHz Windows CE 4.2 devices it takes us only about 20-30 seconds to decompress a 6 MB file (albeit in a ZIP using XCeed ZIP.NET).

Also, if I'm not mistaken, I believe .NET Compact Framework has support for GZip. On a side note, place your FileStreams in using statements to ensure they are closed in the event an exception occurs in the middle of your method.

羁绊已千年 2024-08-28 08:25:33

压缩非常消耗 CPU 资源。也许……这就是需要多长时间。

尝试 DotNetZip - 不确定是否更快,但它是免费的,易于尝试。
DotNetZip 中的 GZipStream 可以替代 .NET BCL 中的 GZipStream。

由于您正在执行 GZIP 而不是 ZIP,因此您只需要 Ionic.Zlib.dll ,而不是 Ionic.Zip.dll 。前者是后者的严格子集。


编辑:代码示例

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
    using (var raw = System.IO.File.Create(outputFile))
    {
        using (Stream compressor = new GZipStream(raw, CompressionMode.Compress))
        {
            byte[] buffer = new byte[WORKING_BUFFER_SIZE];
            int n;
            while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
            {
                compressor.Write(buffer, 0, n);
            }
        }
    }
}

(上述代码中的 GZipStream 由 Ionic.Zlib.dll 提供。)

Compression is very CPU intensive. It may be that ... that's just how long it takes.

Try DotNetZip - not sure if faster, but it is free, easy to try.
There's a GZipStream in DotNetZip that is a replacement for the GZipStream in the .NET BCL.

Since you are doing GZIP and not ZIP, You need only Ionic.Zlib.dll, not Ionic.Zip.dll . The former is a strict subset of the latter.


EDIT: code sample

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
    using (var raw = System.IO.File.Create(outputFile))
    {
        using (Stream compressor = new GZipStream(raw, CompressionMode.Compress))
        {
            byte[] buffer = new byte[WORKING_BUFFER_SIZE];
            int n;
            while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
            {
                compressor.Write(buffer, 0, n);
            }
        }
    }
}

(The GZipStream in the above code is provided by Ionic.Zlib.dll . )

So要识趣 2024-08-28 08:25:33

我使用 ZIP 组件作为 Resco Mobile Toolkit 的一部分: http://www.resco .net/developer/mobileformstoolkit/overview.aspx

该工具包的价格可能令人望而却步 - 这取决于您是否需要任何其他控件。

I use the ZIP component as part of the Resco Mobile Toolkit: http://www.resco.net/developer/mobileformstoolkit/overview.aspx

The price of the toolkit is probably prohibitive - it depends if you have a need for any of the other controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文