我们是五月的花海 , 用青春拥抱时代 |

兴想事成

园龄:12年10个月粉丝:25关注:97

ZIP文件流压缩和解压,本地不产生压缩包

 

   前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题.

比如, 现在从表里面取出数据, 生成xml字符串,然后生成zip文件流,上传到文件服务器.

以前的做法是 string=>  生成xml文件到本地=> 读取本地文件,生成zip文件=> 读取zip文件,存放到服务器.  这样做,本地会有一个xml,一个zip,用完还得删掉!!!

由于是挂到服务器IIS上的, 很多时候,对方并没有开写文件的权限,而且将文件写到本地,需要准备存储空间,好在我的xml,大小一般在10K以内, zip压缩后,就更小了.

几番波折, 找到了一种解决方案, 还是用 " SharpZipLib.Zip ", 本地不产生任何文件. 代码如下

 

复制代码
  /// <summary>
        /// 将文本信息,写入zip压缩包
        /// </summary>
        /// <param name="xmldata">xml文本信息</param>
        /// <param name="xmlFileName">xml文件名称,例如 data.xml</param>
        /// <returns>返回zip压缩文件的字节数组</returns>
        public static byte[] CreateZIPBytes(string xmldata, string xmlFileName)
        {

            try
            {
                byte[] byteArray = System.Text.Encoding.Default.GetBytes(xmldata);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (ZipOutputStream zips = new ZipOutputStream(ms))
                    {
                        zips.SetLevel(9); // 0 - store only to 9 - means best compression

                        var entry = new ZipEntry(xmlFileName);
                        entry.DateTime = DateTime.Now;
                        zips.PutNextEntry(entry);
                        zips.Write(byteArray, 0, byteArray.Length);
                        zips.Finish();
                    }
                    byte[] zipBytes = ms.ToArray(); //这里是一个zip压缩的文件流

                    return zipBytes;
                }

            }
            catch (Exception ex)
            {
                throw ex;

            }
           
        }
复制代码

 

 zip文件流解压

复制代码
 /// <summary>
        /// 传入zip压缩的文件流字节, 得到解压后的第一个文件字节数组
        /// </summary>
        /// <param name="zipbyte">zip文件的文件字节数组</param>
        /// <returns>解压后的第一个文件的文件字节数组</returns>
        public static byte[] UnZIPByBytes(byte[] zipbyte)
        {
            byte[] xmldata = null;
            using (ZipInputStream s = new ZipInputStream(new MemoryStream(zipbyte)))
            {
                ZipEntry theEntry = s.GetNextEntry();
                if (theEntry != null)
                {
                    Console.WriteLine("解压出来的文件名:" + theEntry.Name);

                    using (MemoryStream sa = new MemoryStream())
                    {
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                sa.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        xmldata = sa.ToArray();
                    }
                }
            }
            return xmldata;
        }
复制代码

 

   然后, 生成出来的zip文件流, 经过打包以后, 传递给另外一个Java的项目接口使用, 对方竟然解不出来这个zip的文件流,  顿时感觉, 一群草泥马奔腾.....

 对方使用的是阿帕奇10年前的jar包, 我也不能让对方改包, 所以只能是我这边做调整, 我在网上找了几种其他产生zip的包的方式, 大多数都要产生文件,

然后我问对方, 你们以前跟.net做对接, 他们是怎么做的, 告知我用的是 "Ionic.Zip" 我还是第一次听说这个包, 于是又在网上找了点介绍,看了下. 简单花了点时间,

弄出一下代码, 这下对方可以正常解析了.  

 

复制代码
public class IonicZipHelper
    {
        /// <summary>
        /// 创建zip的压缩文件流
        /// </summary>
        /// <param name="xmldata"></param>
        /// <param name="xmlFileName"></param>
        /// <returns></returns>
        public static byte[] CreateZip(string xmldata, string xmlFileName)
        {
            byte[] xmlByte = Encoding.UTF8.GetBytes(xmldata);
            
            using (MemoryStream ms = new System.IO.MemoryStream())
            {
                using (ZipFile zip = new ZipFile())
                {

                    zip.AddEntry(xmlFileName, xmlByte);
                    zip.Save(ms);

                }
                return ms.ToArray();
            }
        }

        /// <summary>
        /// 解压zip文件流, 得到zip压缩包内的第一个文件的文件流
        /// </summary>
        /// <param name="zipByte"></param>
        /// <returns></returns>
        public static byte[] UnZipByBytes(byte[] zipByte)
        {
            using (MemoryStream msXmlStream = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(zipByte))
                {
                    using (ZipFile zip = ZipFile.Read(ms))
                    {
                        if (zip.Entries.Count > 0)
                        {
                            zip[0].Extract(msXmlStream);
                        }
                    }

                    return msXmlStream.ToArray();
                }
            }

        }
复制代码

 

关于这两种生成zip,和解压缩zip, 我这里也提供源程序 点我下载

 

posted @   兴想事成  阅读(1273)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Good-bye My Loneliness ZARD
  2. 2 Say OK Vanessa Hudgens
  3. 3 All The Love In The World The Corrs
  4. 4 Adesso E Fortuna ~炎と永遠~ 加藤いづみ
Say OK - Vanessa Hudgens
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : BIRGISSON, ARNTHOR/KOTECHA, SAVAN

作曲 : Savan Kotecha/Arnthor Birgisson

Vanessa Hudgens - Say OK

Album: V

You are fine

You are fine

You are fine

You are fine

You are sweet

But I'm still a bit naive with my heart

When you're close I don't breathe

I can't find the words to speak

I feel sparks

But I don't wanna be into you

If you are not looking for true love, oh oh

No I don't wanna start seeing you

If I can't be your only one

So tell me when it's not alright

When it's not ok

Will you try to make me feel better?

Will you say alright? (say alright)

Will you say ok? (Say ok)

Will you stick with me through whatever?

Or run away

(Say that it's gonna be alright)

(That it's gonna be ok)

Say OK

When you call I don't know

If I should pick up the phone every time

I'm not like all my friends

Who keep calling up the boys, I'm so shy

But I don't wanna be into you

If you don't treat me the right way

See I can only start seeing you

If you can make my heart feel safe (feel safe)

When it's not alright

When it's not ok

Will you try to make me feel better?

Will you say alright? (say alright)

Will you say ok? (Say ok)

Will you stick with me through whatever?

Or run away

(Say that it's gonna be alright)

(That it's gonna be ok)

(Don't run away, don't run away)

Let me know if it's gonna be you

Boy, you've got some things to prove

Let me know that you'll keep me safe

I don't want you to run away so

Let me know that you'll call on time

Let me know that you won't be shy

Will you wipe my tears away

Will you hold me closer

When it's not alright

When it's not ok

Will you try to make me feel better

Will you say alright? (say alright)

Will you say ok? (Say ok)

Will you stick with me through whatever?

Or run away

(Say that it's gonna be alright)

(That it's gonna be ok)

Say OK

(Don't run away, don't run away)

(Say that it's gonna be alright)

(That it's gonna be ok)

(Don't run away)

Will you say OK

(Say that it's gonna be alright)

(That it's gonna be ok)

(Don't run away)

You are fine

You are fine