SharpZipLib 解密

cbgn 2009-05-21 03:49:31
有用过SharpZipLib的兄弟吗?
不加密,解密没问题。

加密后,用代码解密失败,提示:Entry compressed size 11 too small for encryption
手工解密也失败。
...全文
369 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
langeyang 2009-07-06
  • 打赏
  • 举报
回复
我用了解密老提示密码错误,无法解密啊?谁能告诉我为什么
cbgn 2009-05-21
  • 打赏
  • 举报
回复
有用加密解密成功的吗
ericzhangbo1982111 2009-05-21
  • 打赏
  • 举报
回复
SharpZipLib 不是有例子吗
我上面就是他们给的列子
cbgn 2009-05-21
  • 打赏
  • 举报
回复
兄弟们,别一贴一大堆,说说怎么回事呀
cbgn 2009-05-21
  • 打赏
  • 举报
回复
这是压缩。

/// <summary>
/// 压缩多层目录
/// </summary>
/// <param name="strFile">压缩目录</param>
/// <param name="s">压缩文件流</param>
/// <param name="staticFile"></param>
public static void ZipFileDirectory(string strDirectory, string zipedFile)
{
using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
s.Password = "aaaaa";
ZipSetp(strDirectory, s, "");
}
}
}

/// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="strFile"></param>
/// <param name="s"></param>
/// <param name="parentPath"></param>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}

Crc32 crc = new Crc32();

string[] filenames = Directory.GetFileSystemEntries(strDirectory);

foreach (string file in filenames)// 遍历所有的文件和目录
{

if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
ZipSetp(file, s, file.Substring(file.LastIndexOf("\\") + 1) + "\\");
}

else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);

string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(fileName);

entry.DateTime = DateTime.Now;
entry.Size = fs.Length;

fs.Close();

crc.Reset();
crc.Update(buffer);

entry.Crc = crc.Value;
s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);
}
}
}
}
ericzhangbo1982111 2009-05-21
  • 打赏
  • 举报
回复
// SharpZipLibrary samples
// Copyright (c) 2007, AlphaSierraPapa
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list
// of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// - Neither the name of the SharpDevelop team nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Text;
using System.Collections;
using System.IO;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;


class MainClass
{
public static void Main(string[] args)
{
// Perform simple parameter checking.
if ( args.Length < 1 ) {
Console.WriteLine("Usage UnzipFile NameOfFile");
return;
}

if ( !File.Exists(args[0]) ) {
Console.WriteLine("Cannot find file '{0}'", args[0]);
return;
}

using (ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]))) {

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {

Console.WriteLine(theEntry.Name);

string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);

// create directory
if ( directoryName.Length > 0 ) {
Directory.CreateDirectory(directoryName);
}

if (fileName != String.Empty) {
using (FileStream streamWriter = File.Create(theEntry.Name)) {

int size = 2048;
byte[] data = new byte[2048];
while (true) {
size = s.Read(data, 0, data.Length);
if (size > 0) {
streamWriter.Write(data, 0, size);
} else {
break;
}
}
streamWriter.Close();
}
}
}
}
}
}
颜氓 2009-05-21
  • 打赏
  • 举报
回复
        /// <summary> 
/// 解压缩文件(压缩文件中含有子目录)
/// </summary>
/// <param name="zipfilepath">待解压缩的文件路径 </param>
/// <param name="unzippath">解压缩到指定目录 </param>
/// <returns>解压后的文件列表 </returns>
public List <string> UnZip(string zipfilepath, string unzippath)
{
//解压出来的文件列表
List <string> unzipFiles = new List <string>();

//检查输出目录是否以“\\”结尾
if (unzippath.EndsWith("\\")==false||unzippath.EndsWith(":\\")==false)
{
unzippath += "\\";
}

ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(unzippath);
string fileName = Path.GetFileName(theEntry.Name);

//生成解压目录【用户解压到硬盘根目录时,不需要创建】
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(directoryName);
}

if (fileName != String.Empty)
{
//如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
if (theEntry.CompressedSize == 0)
break;
//解压文件到指定的目录
directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);
//建立下面的目录和子目录
Directory.CreateDirectory(directoryName);

//记录导出的文件
unzipFiles.Add(unzippath + theEntry.Name);

FileStream streamWriter = File.Create(unzippath + theEntry.Name);

int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();

return unzipFiles;
}
}

}
ericzhangbo1982111 2009-05-21
  • 打赏
  • 举报
回复
// SharpZipLibrary samples
// Copyright (c) 2007, AlphaSierraPapa
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list
// of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// - Neither the name of the SharpDevelop team nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Text;
using System.Collections;
using System.IO;

using ICSharpCode.SharpZipLib.Zip;

class MainClass
{
static public void Main(string[] args)
{
if ( args.Length < 1 ) {
Console.WriteLine("Usage: ZipList file");
return;
}

if ( !File.Exists(args[0]) ) {
Console.WriteLine("Cannot find file");
return;
}

using (ZipFile zFile = new ZipFile(args[0])) {
Console.WriteLine("Listing of : " + zFile.Name);
Console.WriteLine("");
Console.WriteLine("Raw Size Size Date Time Name");
Console.WriteLine("-------- -------- -------- ------ ---------");
foreach (ZipEntry e in zFile) {
DateTime d = e.DateTime;
Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
d.ToString("dd-MM-yy"), d.ToString("HH:mm"),
e.Name);
}
}
}
}
cbgn 2009-05-21
  • 打赏
  • 举报
回复
压缩时加密。
cbgn 2009-05-21
  • 打赏
  • 举报
回复
        /// <summary> 
/// 解压缩一个 zip 文件。
/// </summary>
/// <param name="zipFileName">要解压的 zip 文件。</param>
/// <param name="extractLocation">zip 文件的解压目录。</param>
/// <param name="password">zip 文件的密码。</param>
/// <param name="overWrite">是否覆盖已存在的文件。</param>
public static void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
{

if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\";

using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
s.Password = password;
ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;

if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\";

string fileName = Path.GetFileName(pathToZip);

Directory.CreateDirectory(strDirectory + directoryName);

if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);

if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}

s.Close();
}
}
路人乙e 2009-05-21
  • 打赏
  • 举报
回复
参考下这个 http://www.cnblogs.com/china-liuyang/articles/1198585.html


------------------------------
博客平台:www.cnopenblog.com
路人乙e 2009-05-21
  • 打赏
  • 举报
回复
是解压吧
hesi726 2009-05-21
  • 打赏
  • 举报
回复
贴出你的代码。。用过 SharpZipLib 压缩和解压缩。没有用过加密和解密功能。
非常好用, │ DotNet.Utilities.csproj │ DotNet.Utilities.sln │ DotNet.Utilities.suo │ HttpHelper.cs │ IpHelper.cs │ PDFHelper.cs │ SerializeHelper.cs │ SqlHelper.cs │ ├─bin │ └─Debug │ DotNet.Utilities.dll │ DotNet.Utilities.pdb │ Excel.dll │ HtmlAgilityPack.dll │ ICSharpCode.SharpZipLib.dll │ itextsharp.dll │ Microsoft.Office.Interop.Owc11.dll │ OWC10Chart.dll │ ├─Chart图形 │ Assistant.cs │ OWCChart11.cs │ ├─Cookie&Session │ CookieHelper.cs │ SessionHelper.cs │ SessionHelper2.cs │ ├─FTP操作类 │ edtFTPnet.dll │ FTPClient.cs │ FTPHelper.cs │ FTPOperater.cs │ FTP使用说明.txt │ ├─JSON操作 │ ConvertJson.cs │ ├─JS操作 │ JsHelper.cs │ ├─obj │ └─Debug │ DesignTimeResolveAssemblyReferencesInput.cache │ DotNet.Utilities.csproj.FileListAbsolute.txt │ DotNet.Utilities.dll │ DotNet.Utilities.pdb │ ├─Properties │ AssemblyInfo.cs │ ├─XML操作类 │ XmlHelper.cs │ XMLProcess.cs │ ├─上传下载 │ DownLoadHelper.cs │ FileDown.cs │ FileUp.cs │ UpLoadFiles.cs │ ├─加密解密 │ DEncrypt.cs │ DESEncrypt.cs │ Encrypt.cs │ HashEncode.cs │ MySecurity.cs │ RSACryption.cs │ ├─图片 │ ImageClass.cs │ ImageDown.cs │ ImageUpload.cs │ ├─字符串 │ StringHelper.cs │ ├─导出Excel │ DataToExcel.cs │ ExcelHelper.cs │ ExportExcel.cs │ GridViewExport.cs │ ├─文件操作类 │ DirFile.cs │ FileOperate.cs │ INIFile.cs │ ├─时间戳 │ TimeHelper.cs │ ├─条形码 │ BarCodeToHTML.cs │ ├─正则表达式 │ RegexHelper.cs │ ├─汉字转拼音 │ EcanConvertToCh.cs │ PinYin.cs │ ├─类型转换 │ ConvertHelper.cs │ ├─缓存 │ CacheHelper.cs │ DataCache.cs │ ├─网站安全 │ WebSafe.cs │ 使用说明.txt │ ├─网络 │ NetHelper.cs │ ├─视频转换类 │ VideoConvert.cs │ ├─计划任务 │ IntervalTask.cs │ TimerInfo.cs │ 计划任务使用说明.txt │ ├─配置文件操作类 │ ConfigHelper.cs │ ├─随机数类 │ BaseRandom.cs │ RandomHelper.cs │ RandomOperate.cs │ └─验证码 YZMHelper.cs

110,578

社区成员

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

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

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