本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipOutputStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# ZipOutputStream.Write方法的具体用法?C# ZipOutputStream.Write怎么用?C# ZipOutputStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipOutputStream
的用法示例。
在下文中一共展示了ZipOutputStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: saveFileDialog_FileOk
private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
{
ReportText = ReportTextBox.Text;
ZipEntry ze;
ZipOutputStream zip_out = new ZipOutputStream(File.Create((sender as SaveFileDialog).FileName));
string SourceText;
byte[] data=null;
foreach (string FileName in FileNames)
{
SourceText = VEC.StandartCompiler.GetSourceFileText(FileName);
if (SourceText != null)
{
data = System.Text.Encoding.GetEncoding(1251).GetBytes(SourceText);
ze = new ZipEntry(System.IO.Path.GetFileName(FileName));
zip_out.PutNextEntry(ze);
zip_out.Write(data, 0, data.Length);
}
}
ze = new ZipEntry("Report.txt");
zip_out.PutNextEntry(ze);
data = System.Text.Encoding.GetEncoding(1251).GetBytes(ReportText);
zip_out.Write(data, 0, data.Length);
zip_out.Finish();
zip_out.Close();
}
示例2: ZipFile
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
示例3: ZipFile
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
if (!File.Exists(FileToZip))
{
throw new FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
FileStream fileStream = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
FileStream baseOutputStream = File.Create(ZipedFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(baseOutputStream);
ZipEntry entry = new ZipEntry("ZippedFile");
zipOutputStream.PutNextEntry(entry);
zipOutputStream.SetLevel(CompressionLevel);
byte[] array = new byte[BlockSize];
int num = fileStream.Read(array, 0, array.Length);
zipOutputStream.Write(array, 0, num);
try
{
while ((long)num < fileStream.Length)
{
int num2 = fileStream.Read(array, 0, array.Length);
zipOutputStream.Write(array, 0, num2);
num += num2;
}
}
catch (Exception ex)
{
throw ex;
}
zipOutputStream.Finish();
zipOutputStream.Close();
fileStream.Close();
}
示例4: ZipFiles
/// <summary>
/// Método que faz o zip de arquivos encontrados no diretório <strPathDirectory>
/// </summary>
/// <param name="strPath"></param>
public static void ZipFiles(String strPathDirectory, String strZipName)
{
try
{
using (ZipOutputStream ZipOut = new ZipOutputStream(File.Create(strPathDirectory + "\\" + strZipName + ".zip")))
{
string[] OLfiles = Directory.GetFiles(strPathDirectory);
Console.WriteLine(OLfiles.Length);
ZipOut.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string filename in OLfiles)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(filename));
ZipOut.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filename))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
ZipOut.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
ZipOut.Finish();
ZipOut.Close();
}
}
catch (System.Exception ex)
{
System.Console.Error.WriteLine("exception: " + ex);
//TODO colocar log
}
}
示例5: AddZip
public static string AddZip(string fileName, string zipName, ZipOutputStream s)
{
Crc32 crc = new Crc32();
try
{
FileStream fs = File.OpenRead(fileName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fileName = Path.GetFileName(fileName);
long fileLength = fs.Length;
fs.Close();
ZipEntry entry = new ZipEntry(zipName);
entry.DateTime = DateTime.Now;
entry.Size = fileLength;
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
return string.Empty;
}
catch (Exception addEx)
{
return addEx.ToString();
}
}
示例6: ZipFileDirectory
private static void ZipFileDirectory(string[] files, ZipOutputStream z)
{
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
foreach(string file in files)
{
fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string fileName = Path.GetFileName(file);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
z.PutNextEntry(entry);
z.Write(buffer, 0, buffer.Length);
}
}
finally
{
if(fs!=null)
{
fs.Close();
fs = null;
}
GC.Collect();
}
}
示例7: CompressBuffer
/// <summary>
/// Compresses an array of bytes and stores the result in a new array of bytes.
/// </summary>
/// <param name="uncompressed">The uncompressed buffer.</param>
/// <param name="compressed">An array of bytes where the compressed input will be stored.</param>
/// <remarks>
/// The compressed input is passed back to the calling method as an <b>out</b>
/// parameter. That means that the calling method doesn't need to initialize the
/// compressed buffer.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown if the uncompressed input buffer is empty or null.
/// </exception>
/// <exception cref="CWZipException">
/// Thrown if a problem is encountered during the compression process.
/// </exception>
public static void CompressBuffer(byte[] uncompressed, out byte[] compressed)
{
if ((uncompressed == null) || (uncompressed.Length == 0))
{
throw new ArgumentNullException("uncompressed", "The uncompressed input buffer cannot be null or empty.");
}
MemoryStream ms = null;
compressed = null;
try
{
ms = new MemoryStream();
ZipOutputStream zip = new ZipOutputStream(ms);
zip.SetLevel(compressionLevel);
ZipEntry entry = new ZipEntry("1");
zip.PutNextEntry(entry);
zip.Write(uncompressed, 0, uncompressed.Length);
zip.Finish();
ms.Position = 0;
compressed = ms.ToArray();
ms.Close();
}
catch (Exception e)
{
if (ms != null)
{
ms.Close();
}
throw new CWZipException(e.Message);
}
finally
{
ms = null;
GC.Collect();
}
}
示例8: WriteObjectToZip
private void WriteObjectToZip (ZipOutputStream zip_out,
FileSystemObject fso,
EventTracker tracker)
{
MemoryStream memory = null;
string name;
name = fso.FullName.Substring (this.FullName.Length + 1);
if (fso is DirectoryObject)
name += "/";
ZipEntry entry;
entry = new ZipEntry (name);
entry.DateTime = fso.Timestamp;
if (fso is DirectoryObject)
entry.Size = 0;
else {
memory = new MemoryStream ();
((FileObject) fso).AddToStream (memory, tracker);
entry.Size = memory.Length;
}
zip_out.PutNextEntry (entry);
if (memory != null) {
zip_out.Write (memory.ToArray (), 0, (int) memory.Length);
memory.Close ();
}
// If this is a directory, write out the children
if (fso is DirectoryObject)
foreach (FileSystemObject child in fso.Children)
WriteObjectToZip (zip_out, child, tracker);
}
示例9: zip
public void zip(string to, string[] files)
{
using (ZipOutputStream s = new ZipOutputStream(File.Create(to)))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in files)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file)) { 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();
}
}
示例10: ZipFile
//public static void ZipFile(string path, string file2Zip, string zipFileName, string zip, string bldgType)
public static void ZipFile(string path, string file2Zip, string zipFileName)
{
//MemoryStream ms = InitializeGbxml(path + file2Zip, zip, bldgType) as MemoryStream;
MemoryStream ms = InitializeGbxml(Path.Combine(path , file2Zip)) as MemoryStream;
string compressedFile =Path.Combine(path, zipFileName);
if (File.Exists(compressedFile))
{
File.Delete(compressedFile);
}
Crc32 objCrc32 = new Crc32();
ZipOutputStream strmZipOutputStream = new ZipOutputStream(File.Create(compressedFile));
strmZipOutputStream.SetLevel(9);
byte[] gbXmlBuffer = new byte[ms.Length];
ms.Read(gbXmlBuffer, 0, gbXmlBuffer.Length);
ZipEntry objZipEntry = new ZipEntry(file2Zip);
objZipEntry.DateTime = DateTime.Now;
objZipEntry.Size = ms.Length;
ms.Close();
objCrc32.Reset();
objCrc32.Update(gbXmlBuffer);
objZipEntry.Crc = objCrc32.Value;
strmZipOutputStream.PutNextEntry(objZipEntry);
strmZipOutputStream.Write(gbXmlBuffer, 0, gbXmlBuffer.Length);
strmZipOutputStream.Finish();
strmZipOutputStream.Close();
strmZipOutputStream.Dispose();
}
示例11: ZipFiles
/// <summary>
/// Zip all the specified files into the specified ZipFileName.
/// </summary>
public static string ZipFiles(IEnumerable<string> FilesToZip, string ZipFileName, string Password)
{
if (!File.Exists(ZipFileName))
File.Delete(ZipFileName);
ZipOutputStream Zip = new ZipOutputStream(File.Create(ZipFileName));
if (Password != "")
Zip.Password = Password;
try
{
Zip.SetLevel(5); // 0 - store only to 9 - means best compression
foreach (string FileName in FilesToZip)
{
FileStream fs = File.OpenRead(FileName);
byte[] Buffer = new byte[fs.Length];
fs.Read(Buffer, 0, Buffer.Length);
fs.Close();
ZipEntry Entry = new ZipEntry(Path.GetFileName(FileName));
Zip.PutNextEntry(Entry);
Zip.Write(Buffer, 0, Buffer.Length);
}
Zip.Finish();
Zip.Close();
return ZipFileName;
}
catch (System.Exception)
{
Zip.Finish();
Zip.Close();
File.Delete(ZipFileName);
throw;
}
}
示例12: GetZipFileName
public static string GetZipFileName(string filename)
{
string zipfile = filename.Replace (".db", ".zip");
try {
using (ZipOutputStream s = new ZipOutputStream (File.Create (zipfile))) {
s.SetLevel (9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
ZipEntry entry = new ZipEntry (filename);
entry.DateTime = DateTime.Now;
s.PutNextEntry (entry);
using (FileStream fs = File.OpenRead (filename)) {
int sourceBytes;
do {
sourceBytes = fs.Read (buffer, 0, buffer.Length);
s.Write (buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
s.Finish ();
s.Close ();
}
} catch (Exception ex) {
zipfile = filename;
}
return zipfile;
}
示例13: 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);
}
示例14: 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;
}
}
示例15: CreateFileZip
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <param name="destinationZipFilePath">保存压缩文件的文件名</param>
/// <param name="level">压缩文件等级</param>
/// <returns>返回-2说明被压缩文件已经存在,返回1说明压缩成功</returns>
public static int CreateFileZip(string sourceFilePath, string destinationZipFilePath, int level)
{
if (!Directory.Exists(destinationZipFilePath.Substring(0, destinationZipFilePath.LastIndexOf("\\"))))
{
Directory.CreateDirectory(destinationZipFilePath.Substring(0, destinationZipFilePath.LastIndexOf("\\")));
}
if (File.Exists(destinationZipFilePath))
{
return -2;
}
else
{
ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
zipStream.SetLevel(level); // 压缩级别 0-9
Crc32 crc = new Crc32();
FileStream fileStream = File.OpenRead(sourceFilePath);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
string tempFile = sourceFilePath.Substring(sourceFilePath.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempFile);
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
zipStream.Finish();
zipStream.Close();
return 1;
}
}