public ZipEntry AddEntry(string entryName, WriteDelegate writer)
{
ZipEntry ze = ZipEntry.CreateForWriter(entryName, writer);
if (this.Verbose)
{
this.StatusMessageTextWriter.WriteLine("adding {0}...", entryName);
}
return this._InternalAddEntry(ze);
}
public void Save()
{
try
{
bool flag = false;
this._saveOperationCanceled = false;
this._numberOfSegmentsForMostRecentSave = 0;
this.OnSaveStarted();
if (this.WriteStream == null)
{
throw new BadStateException("You haven't specified where to save the zip.");
}
if (((this._name != null) && this._name.EndsWith(".exe")) && !this._SavingSfx)
{
throw new BadStateException("You specified an EXE for a plain zip file.");
}
if (!this._contentsChanged)
{
this.OnSaveCompleted();
if (this.Verbose)
{
this.StatusMessageTextWriter.WriteLine("No save is necessary....");
}
}
else
{
this.Reset(true);
if (this.Verbose)
{
this.StatusMessageTextWriter.WriteLine("saving....");
}
if ((this._entries.Count >= 0xffff) && (this._zip64 == Zip64Option.Default))
{
throw new ZipException("The number of entries is 65535 or greater. Consider setting the UseZip64WhenSaving property on the ZipFile instance.");
}
int current = 0;
ICollection<ZipEntry> entries = this.SortEntriesBeforeSaving ? this.EntriesSorted : this.Entries;
foreach (ZipEntry entry in entries)
{
this.OnSaveEntry(current, entry, true);
entry.Write(this.WriteStream);
if (this._saveOperationCanceled)
{
break;
}
current++;
this.OnSaveEntry(current, entry, false);
if (this._saveOperationCanceled)
{
break;
}
if (entry.IncludedInMostRecentSave)
{
flag |= entry.OutputUsedZip64.Value;
}
}
if (!this._saveOperationCanceled)
{
ZipSegmentedStream writeStream = this.WriteStream as ZipSegmentedStream;
this._numberOfSegmentsForMostRecentSave = (writeStream != null) ? writeStream.CurrentSegment : 1;
bool flag2 = ZipOutput.WriteCentralDirectoryStructure(this.WriteStream, entries, this._numberOfSegmentsForMostRecentSave, this._zip64, this.Comment, new ZipContainer(this));
this.OnSaveEvent(ZipProgressEventType.Saving_AfterSaveTempArchive);
this._hasBeenSaved = true;
this._contentsChanged = false;
flag |= flag2;
this._OutputUsesZip64 = new bool?(flag);
if ((this._name != null) && ((this._temporaryFileName != null) || (writeStream != null)))
{
this.WriteStream.Dispose();
if (this._saveOperationCanceled)
{
return;
}
if (this._fileAlreadyExists && (this._readstream != null))
{
this._readstream.Close();
this._readstream = null;
foreach (ZipEntry entry2 in entries)
{
ZipSegmentedStream stream2 = entry2._archiveStream as ZipSegmentedStream;
if (stream2 != null)
{
stream2.Dispose();
}
entry2._archiveStream = null;
}
}
string path = null;
if (File.Exists(this._name))
{
path = this._name + "." + Path.GetRandomFileName();
if (File.Exists(path))
{
this.DeleteFileWithRetry(path);
}
File.Move(this._name, path);
}
this.OnSaveEvent(ZipProgressEventType.Saving_BeforeRenameTempArchive);
File.Move((writeStream != null) ? writeStream.CurrentTempName : this._temporaryFileName, this._name);
this.OnSaveEvent(ZipProgressEventType.Saving_AfterRenameTempArchive);
if (path != null)
{
try
{
if (File.Exists(path))
{
File.Delete(path);
}
}
catch
{
}
}
this._fileAlreadyExists = true;
}
NotifyEntriesSaveComplete(entries);
this.OnSaveCompleted();
this._JustSaved = true;
}
}
}
finally
{
this.CleanupAfterSaveOperation();
}
}
public static bool IsZipFile(Stream stream, bool testExtract)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
bool flag = false;
try
{
if (!stream.CanRead)
{
return false;
}
Stream @null = Stream.Null;
using (ZipFile file = Read(stream, null, null, null))
{
if (testExtract)
{
foreach (ZipEntry entry in file)
{
if (!entry.IsDirectory)
{
entry.Extract(@null);
}
}
}
}
flag = true;
}
catch (IOException)
{
}
catch (ZipException)
{
}
return flag;
}
private static ZipFile Read(Stream zipStream, TextWriter statusMessageWriter, Encoding encoding, EventHandler<ReadProgressEventArgs> readProgress)
{
if (zipStream == null)
{
throw new ArgumentNullException("zipStream");
}
ZipFile zf = new ZipFile {
_StatusMessageTextWriter = statusMessageWriter,
_alternateEncoding = encoding ?? DefaultEncoding,
_alternateEncodingUsage = ZipOption.Always
};
if (readProgress != null)
{
zf.ReadProgress += readProgress;
}
zf._readstream = (zipStream.Position == 0L) ? zipStream : new OffsetStream(zipStream);
zf._ReadStreamIsOurs = false;
if (zf.Verbose)
{
zf._StatusMessageTextWriter.WriteLine("reading from stream...");
}
ReadIntoInstance(zf);
return zf;
}
/// <summary>
/// 压缩ZIP文件
/// 支持多文件和多目录,或是多文件和多目录一起压缩
/// </summary>
/// <param name="list">待压缩的文件或目录集合</param>
/// <param name="strZipName">压缩后的文件名</param>
/// <param name="isDirStruct">是否按目录结构压缩</param>
/// <returns>成功:true/失败:false</returns>
public static bool CompressMulti(List<string> list, string strZipName, bool isDirStruct)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (string.IsNullOrEmpty(strZipName))
{
throw new ArgumentNullException(strZipName);
}
try
{
//设置编码,解决压缩文件时中文乱码
using (var zip = new ZipFile(Encoding.Default))
{
foreach (var path in list)
{
//取目录名称
var fileName = Path.GetFileName(path);
//如果是目录
if (Directory.Exists(path))
{
//按目录结构压缩
if (isDirStruct)
{
zip.AddDirectory(path, fileName);
}
else
{
//目录下的文件都压缩到Zip的根目录
zip.AddDirectory(path);
}
}
if (File.Exists(path))
{
zip.AddFile(path);
}
}
//压缩
zip.Save(strZipName);
return true;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 解压ZIP文件
/// </summary>
/// <param name="strZipPath">待解压的ZIP文件</param>
/// <param name="strUnZipPath">解压的目录</param>
/// <param name="overWrite">是否覆盖</param>
/// <returns>成功:true/失败:false</returns>
public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
{
if (string.IsNullOrEmpty(strZipPath))
{
throw new ArgumentNullException(strZipPath);
}
if (string.IsNullOrEmpty(strUnZipPath))
{
throw new ArgumentNullException(strUnZipPath);
}
try
{
var options = new ReadOptions
{
Encoding = Encoding.Default
};
//设置编码,解决解压文件时中文乱码
using (var zip = ZipFile.Read(strZipPath, options))
{
foreach (var entry in zip)
{
if (string.IsNullOrEmpty(strUnZipPath))
{
strUnZipPath = strZipPath.Split('.').First();
}
entry.Extract(strUnZipPath,overWrite
? ExtractExistingFileAction.OverwriteSilently
: ExtractExistingFileAction.DoNotOverwrite);
}
return true;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 得到指定的输入流的ZIP压缩流对象
/// </summary>
/// <param name="sourceStream">源数据流</param>
/// <param name="entryName">实体名称</param>
/// <returns></returns>
public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
{
if (sourceStream == null)
{
throw new ArgumentNullException("sourceStream");
}
var compressedStream = new MemoryStream();
long sourceOldPosition = 0;
try
{
sourceOldPosition = sourceStream.Position;
sourceStream.Position = 0;
using (var zip = new ZipFile())
{
zip.AddEntry(entryName, sourceStream);
zip.Save(compressedStream);
compressedStream.Position = 0;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
try
{
sourceStream.Position = sourceOldPosition;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
return compressedStream;
}
/// <summary>
/// 得到指定的字节数组的ZIP解压流对象
/// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static Stream ZipDecompress(byte[] data)
{
Stream decompressedStream = new MemoryStream();
if (data == null) return decompressedStream;
try
{
var dataStream = new MemoryStream(data);
using (var zip = ZipFile.Read(dataStream))
{
if (zip.Entries.Count > 0)
{
zip.Entries.First().Extract(decompressedStream);
// Extract方法中会操作ms,后续使用时必须先将Stream位置归零,否则会导致后续读取不到任何数据
// 返回该Stream对象之前进行一次位置归零动作
decompressedStream.Position = 0;
}
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
return decompressedStream;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有