本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipEntry类的典型用法代码示例。如果您正苦于以下问题:C# ZipEntry类的具体用法?C# ZipEntry怎么用?C# ZipEntry使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZipEntry类属于ICSharpCode.SharpZipLib.Zip命名空间,在下文中一共展示了ZipEntry类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Save
public void Save(string extPath)
{
// https://forums.xamarin.com/discussion/7499/android-content-getexternalfilesdir-is-it-available
Java.IO.File sd = Android.OS.Environment.ExternalStorageDirectory;
//FileStream fsOut = File.Create(sd.AbsolutePath + "/Android/data/com.FSoft.are_u_ok_/files/MoodData.zip");
FileStream fsOut = File.Create(extPath + "/MoodData.zip");
//https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples
ZipOutputStream zipStream = new ZipOutputStream(fsOut);
zipStream.SetLevel (3); //0-9, 9 being the highest level of compression
zipStream.Password = "Br1g1tte"; // optional. Null is the same as not setting. Required if using AES.
ZipEntry newEntry = new ZipEntry ("Mood.csv");
newEntry.IsCrypted = true;
zipStream.PutNextEntry (newEntry);
// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
byte[ ] buffer = new byte[4096];
string filename = extPath + "/MoodData.csv";
using (FileStream streamReader = File.OpenRead(filename)) {
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry ();
zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
zipStream.Close ();
}
示例2: CreateZipFile
public static void CreateZipFile(string[] filenames, string outputFile)
{
// Zip up the files - From SharpZipLib Demo Code
using (ZipOutputStream s = new ZipOutputStream(File.Create(outputFile)))
{
s.SetLevel(9); // 0-9, 9 being the highest level of compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
}
while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
示例3: ZipDir
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="dirToZip"></param>
/// <param name="zipedFileName"></param>
/// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param>
public void ZipDir(string dirToZip, string zipedFileName, int compressionLevel = 9)
{
if (Path.GetExtension(zipedFileName) != ".zip")
{
zipedFileName = zipedFileName + ".zip";
}
using (var zipoutputstream = new ZipOutputStream(File.Create(zipedFileName)))
{
zipoutputstream.SetLevel(compressionLevel);
var crc = new Crc32();
var fileList = GetAllFies(dirToZip);
foreach (DictionaryEntry item in fileList)
{
var fs = new FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
// ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(dirToZip.Length + 1));
var entry = new ZipEntry(Path.GetFileName(item.Key.ToString()))
{
DateTime = (DateTime) item.Value,
Size = fs.Length
};
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, 0, buffer.Length);
}
}
}
示例4: ZipFiles
public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
{
ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
// find number of chars to remove // from orginal file path
TrimLength += 1; //remove '\'
FileStream ostream;
byte[] obuffer;
string outPath = outputPathAndFile;
ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
if (password != null && password != String.Empty)
oZipStream.Password = password;
oZipStream.SetLevel(9); // maximum compression
ZipEntry oZipEntry;
foreach (string Fil in ar) // for each file, generate a zipentry
{
oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
oZipStream.PutNextEntry(oZipEntry);
if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
{
ostream = File.OpenRead(Fil);
obuffer = new byte[ostream.Length];
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
}
}
oZipStream.Finish();
oZipStream.Close();
oZipStream.Dispose();
}
示例5: CreateToMemoryStream
private static void CreateToMemoryStream(IEnumerable<Tuple<string, Stream>> entries, string zipName)
{
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
foreach (var entry in entries)
{
ZipEntry newEntry = new ZipEntry(entry.Item1);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(entry.Item2, zipStream, new byte[4096]);
zipStream.CloseEntry();
}
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
outputMemStream.Position = 0;
File.WriteAllBytes(zipName, outputMemStream.ToArray());
//// Alternative outputs:
//// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
//byte[] byteArrayOut = outputMemStream.ToArray();
//// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
//byte[] byteArrayOut = outputMemStream.GetBuffer();
//long len = outputMemStream.Length;
}
示例6: zip
private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, staticFile);
}
else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
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);
}
}
}
示例7: Zip
public static void Zip(string strFile, string strZipFile)
{
Crc32 crc1 = new Crc32();
ZipOutputStream stream1 = new ZipOutputStream(File.Create(strZipFile));
try
{
stream1.SetLevel(6);
FileStream stream2 = File.OpenRead(strFile);
byte[] buffer1 = new byte[stream2.Length];
stream2.Read(buffer1, 0, buffer1.Length);
ZipEntry entry1 = new ZipEntry(strFile.Split(new char[] { '\\' })[strFile.Split(new char[] { '\\' }).Length - 1]);
entry1.DateTime = DateTime.Now;
entry1.Size = stream2.Length;
stream2.Close();
crc1.Reset();
crc1.Update(buffer1);
entry1.Crc = crc1.Value;
stream1.PutNextEntry(entry1);
stream1.Write(buffer1, 0, buffer1.Length);
}
catch (Exception exception1)
{
throw exception1;
}
finally
{
stream1.Finish();
stream1.Close();
stream1 = null;
crc1 = null;
}
}
示例8: Decompress
public void Decompress(ZipEntry file, Stream str) {
int size;
while (true) {
size = zipIn.Read(buf, 0, buf.Length); if (size <= 0) break;
str.Write(buf, 0, (int)size);
}
}
示例9: Write
// See this link for details on zipping using SharpZipLib: https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#wiki-anchorCreate
public void Write(Cookbookology.Domain.Cookbook cookbook, Stream outputStream)
{
if (cookbook == null) throw new ArgumentNullException("cookbook");
if (outputStream == null) throw new ArgumentNullException("outputStream");
var converter = new MyCookbookConverter();
var mcb = converter.ConvertFromCommon(cookbook);
var ms = new MemoryStream();
var s = new XmlSerializer(typeof(Cookbook));
s.Serialize(ms, mcb);
ms.Position = 0; // reset to the start so that we can write the stream
// Add the cookbook as a single compressed file in a Zip
using (var zipStream = new ZipOutputStream(outputStream))
{
zipStream.SetLevel(3); // compression
zipStream.UseZip64 = UseZip64.Off; // not compatible with all utilities and OS (WinXp, WinZip8, Java, etc.)
var entry = new ZipEntry(mcbFileName);
entry.DateTime = DateTime.Now;
zipStream.PutNextEntry(entry);
StreamUtils.Copy(ms, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // Don't close the outputStream (parameter)
zipStream.Close();
}
}
示例10: diskLess
public byte[] diskLess()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine("HELLO!");
sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
// create the ZipEntry archive from the txt file in memory stream ms
MemoryStream outputMS = new System.IO.MemoryStream();
ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
ZipEntry ze = new ZipEntry(@"dir1/dir2/whatever.txt");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
byte[] byteArrayOut = outputMS.ToArray();
outputMS.Close();
ms.Close();
return byteArrayOut;
}
示例11: ClsZipFileItem
public ClsZipFileItem(string zipPath, ZipEntry entry)
: base(entry.Name, entry.DateTime)
{
_path = entry.Name;
_entry = entry;
_zipPath = zipPath;
}
示例12: CompressFilesToOneZipFile
private void CompressFilesToOneZipFile(ICollection<string> inputPaths, string zipFilePath)
{
Log.LogMessage(MessageImportance.Normal, "Zipping " + inputPaths.Count + " files to zip file " + zipFilePath);
using (var fsOut = File.Create(zipFilePath)) // Overwrites previous file
{
using (var zipStream = new ZipOutputStream(fsOut))
{
foreach (var inputPath in inputPaths)
{
zipStream.SetLevel(9); // Highest level of compression
var inputFileInfo = new FileInfo(inputPath);
var newEntry = new ZipEntry(inputFileInfo.Name) { DateTime = inputFileInfo.CreationTime };
zipStream.PutNextEntry(newEntry);
var buffer = new byte[4096];
using (var streamReader = File.OpenRead(inputPath))
{
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
zipStream.IsStreamOwner = true;
zipStream.Close();
}
}
}
示例13: CompressContent
/// <summary>
/// Compress an string using ZIP
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static byte[] CompressContent(string contentToZip)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] buff = encoding.GetBytes(contentToZip);
try
{
using (MemoryStream stream = new MemoryStream())
{
using (ZipOutputStream zipout = new ZipOutputStream(stream))
{
zipout.SetLevel(9);
ZipEntry entry = new ZipEntry("zipfile.zip");
entry.DateTime = DateTime.Now;
zipout.PutNextEntry(entry);
zipout.Write(buff, 0, buff.Length);
zipout.Finish();
byte[] outputbyte = new byte[(int)stream.Length];
stream.Position = 0;
stream.Read(outputbyte, 0, (int)stream.Length);
return outputbyte;
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
return null;
}
}
示例14: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
string fileName = Path.GetTempFileName();
var response = context.HttpContext.Response;
using (var zipOutputStream = new ZipOutputStream(new FileStream(fileName, FileMode.OpenOrCreate)))
{
zipOutputStream.SetLevel(0);
foreach (var photo in Photos)
{
//FileInfo fileInfo = new FileInfo(photo.MediaFilePath);
ZipEntry entry = new ZipEntry(Tag.Name + @"\" + photo.Id + ".jpg");
zipOutputStream.PutNextEntry(entry);
using (FileStream fs = System.IO.File.OpenRead(photo.MediaFilePath))
{
byte[] buff = new byte[1024];
int n = 0;
while ((n = fs.Read(buff, 0, buff.Length)) > 0)
zipOutputStream.Write(buff, 0, n);
}
}
zipOutputStream.Finish();
}
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
response.Clear();
response.AddHeader("Content-Disposition", "attachment; filename=" + "Photos.zip");
response.AddHeader("Content-Length", file.Length.ToString());
response.ContentType = "application/octet-stream";
response.WriteFile(file.FullName);
response.End();
System.IO.File.Delete(fileName);
}
示例15: CompressFolder
private static void CompressFolder(string path, ZipOutputStream zipStream)
{
string[] files = Directory.GetFiles(path);
foreach (string filename in files)
{
FileInfo fi = new FileInfo(filename);
int offset = _root.Length + 3;
string entryName = filename.Substring(offset);
entryName = ZipEntry.CleanName(entryName);
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
string[] folders = Directory.GetDirectories(path);
foreach (string folder in folders)
{
CompressFolder(folder, zipStream);
}
}