C#无法对压缩包解压缩

李敢敢 2019-06-10 05:56:12

搜狗api下载的压缩包。。c#怎么解压缩呀。。 SharpCompress SharpZipLib 都无法解压。。是因为格式不对吗?求大佬解惑
...全文
289 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jx315425246 2019-06-11
  • 打赏
  • 举报
回复
我想先了解entry这个类方法和savedirpath的值,不然无法作出判断
李敢敢 2019-06-11
  • 打赏
  • 举报
回复
public class GZipHelper
    {
        public static byte[] Compress(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var stream = new GZipStream(ms, CompressionMode.Compress, true);
                stream.Write(data, 0, data.Length);
                stream.Close();
                return ms.ToArray();
            }
        }

        public static byte[] Decompress(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (Stream inStream = new GZipStream(ms,CompressionMode.Decompress))
                using (MemoryStream outStream = new MemoryStream())
                {
                    byte[] buffer = new byte[4096];
                    int size = inStream.Read(buffer, 0, (int)buffer.Length);
                    while (size > 0)
                    {
                        outStream.Write(buffer, 0, size);
                        size = inStream.Read(buffer, 0, (int)buffer.Length);
                    }
                    //outStream.Write(buffer, outStream, buffer);
                    return outStream.ToArray();
                }
            }
        }
    }
李敢敢 2019-06-11
  • 打赏
  • 举报
回复
引用 11 楼 wanghui0380 的回复:
1.首先看包格式,zip,7z------这类可以用一般的解压代码解,rar还在专利保护期所以rar必须使用rar官方的类解。 2.文件必须存在,你需要检查文件是否存在 3.文件必须无占用,所以你得检查文件是否可以读写。 从你的截图上看,我们认为你已经得到了Entrys 并能进入循环,代表你能解开他。现在你要写到指定的目录,报错“值不能为null”,这表示我们第一件事情是检查参数是否正确
感谢大佬,已解决
李敢敢 2019-06-11
  • 打赏
  • 举报
回复
引用 9 楼 娃都会打酱油了 的回复:
用的SharpCompress这个lib,Gzip部分是nfx原生的
感谢大佬,已解压,例子很完整
wanghui0380 2019-06-11
  • 打赏
  • 举报
回复
1.首先看包格式,zip,7z------这类可以用一般的解压代码解,rar还在专利保护期所以rar必须使用rar官方的类解。 2.文件必须存在,你需要检查文件是否存在 3.文件必须无占用,所以你得检查文件是否可以读写。 从你的截图上看,我们认为你已经得到了Entrys 并能进入循环,代表你能解开他。现在你要写到指定的目录,报错“值不能为null”,这表示我们第一件事情是检查参数是否正确
  • 打赏
  • 举报
回复
用的SharpCompress这个lib,Gzip部分是nfx原生的
  • 打赏
  • 举报
回复
这是GZIP压缩
            var filePath = "20190610_173253.csv.zip";
            
            using (Stream stream = File.OpenRead(filePath))
            {
                var reader = ReaderFactory.Open(stream);
                ArchiveType type = reader.ArchiveType;
                while (reader.MoveToNextEntry())
                {
                    if (!reader.Entry.IsDirectory)
                    {
                        Console.WriteLine(reader.Entry.CompressionType);
                    }
                }
            }
            var bytes = GZipHelper.Decompress(File.ReadAllBytes(filePath));
            File.WriteAllBytes("ddd.csv", bytes);
    public class GZipHelper
    {
        public static byte[] Compress(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var stream = new GZipStream(ms, CompressionMode.Compress, true);
                stream.Write(data, 0, data.Length);
                stream.Close();
                return ms.ToArray();
            }
        }

        public static byte[] Decompress(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (Stream inStream = new GZipInputStream(ms))
                using (MemoryStream outStream = new MemoryStream())
                {
                    byte[] buffer = new byte[4096];
                    StreamUtils.Copy(inStream, outStream, buffer);
                    return outStream.ToArray();
                }
            }
        }
    
    }
李敢敢 2019-06-10
  • 打赏
  • 举报
回复
引用 6 楼 正怒月神 的回复:
空引用,你看看传的saveDirPath是不是空啊
saveDirPath不是空的,截图里有监视值
正怒月神 2019-06-10
  • 打赏
  • 举报
回复
空引用,你看看传的saveDirPath是不是空啊
李敢敢 2019-06-10
  • 打赏
  • 举报
回复
引用 1 楼 娃都会打酱油了 的回复:
你的包呢?好歹给包才能知道是用的什么压缩方式啊
http://118.89.54.105/test/20190610_173253.csv.zip
李敢敢 2019-06-10
  • 打赏
  • 举报
回复
引用 2 楼 stherix 的回复:
先看看是不是zip压缩包,提示参数是null。你手工检查是不是能够解压,文件是否正常
能解压的
李敢敢 2019-06-10
  • 打赏
  • 举报
回复
引用 1 楼 娃都会打酱油了 的回复:
你的包呢?好歹给包才能知道是用的什么压缩方式啊
包不知道怎么上传。。。
stherix 2019-06-10
  • 打赏
  • 举报
回复
先看看是不是zip压缩包,提示参数是null。你手工检查是不是能够解压,文件是否正常
  • 打赏
  • 举报
回复
你的包呢?好歹给包才能知道是用的什么压缩方式啊

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧