求C#解压方法

qq_16918789 2016-12-01 09:22:14
下面给的事java 解密方法,希望有人帮忙给出对应的C#解密,加密方法
private static String unzipStr(String inputXml)
{
ByteArrayOutputStream out;
ByteArrayInputStream in;
ZipInputStream zin;
out = null;
in = null;
zin = null;
String decompressed;
try
{
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream((new BASE64Decoder()).decodeBuffer(inputXml));
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
byte buffer[] = new byte[1024];
for (int offset = -1; (offset = zin.read(buffer)) != -1;)
out.write(buffer, 0, offset);

decompressed = new String(out.toByteArray(), "UTF-8");

}
catch (IOException e)
{
decompressed = inputXml;
}
if (zin != null)
try
{
zin.close();
}
catch (IOException ioexception) { }
if (in != null)
try
{
in.close();
}
catch (IOException ioexception1) { }
if (out != null)
try
{
out.close();
}
catch (IOException ioexception2) { }

if (zin != null)
try
{
zin.close();
}
catch (IOException ioexception3) { }
if (in != null)
try
{
in.close();
}
catch (IOException ioexception4) { }
if (out != null)
try
{
out.close();
}
catch (IOException ioexception5) { }

if (zin != null)
try
{
zin.close();
}
catch (IOException ioexception6) { }
if (in != null)
try
{
in.close();
}
catch (IOException ioexception7) { }
if (out != null)
try
{
out.close();
}
catch (IOException ioexception8) { }
return decompressed;
}
...全文
150 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

using ICSharpCode.SharpZipLib.Zip;
    public class ZipHelper
    {
        public static void Zip(string zipFilePath, string passWord = null, params string[] files)
        {
            using (var fStream = File.Create(zipFilePath))
            {
                using (var zoStream = new ZipOutputStream(fStream))
                {
                    if (!string.IsNullOrWhiteSpace(passWord))
                    {
                        zoStream.Password = passWord;
                    }
                    var buffer = new byte[4096];
                    foreach (var file in files)
                    {
                        var entry = new ZipEntry(Path.GetFileName(file));
                        zoStream.PutNextEntry(entry);
                        using (var fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                zoStream.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    zoStream.Finish();
                }
            }
        }

        public static IList<string> UnZip(string zipFilePath, string unZipDirectory, string passWord = null)
        {
            IList<string> list = new List<string>();
            using (var ziStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                if (!string.IsNullOrWhiteSpace(passWord))
                {
                    ziStream.Password = passWord;
                }
                ZipEntry theEntry;
                while ((theEntry = ziStream.GetNextEntry()) != null)
                {
                    if (!Directory.Exists(unZipDirectory))
                    {
                        Directory.CreateDirectory(unZipDirectory);
                    }
                    string path = Path.Combine(unZipDirectory, theEntry.Name);
                    using (var streamWriter = File.Create(path))
                    {
                        list.Add(path);
                        var data = new byte[2048];
                        while (true)
                        {
                            var size = ziStream.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return list;
        }
    }
衣舞晨风 2016-12-01
  • 打赏
  • 举报
回复
http://blog.csdn.net/jiankunking/article/details/38070875 C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩

110,536

社区成员

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

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

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