本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipInputStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# ZipInputStream.Read方法的具体用法?C# ZipInputStream.Read怎么用?C# ZipInputStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipInputStream
的用法示例。
在下文中一共展示了ZipInputStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtractArchive
public static string ExtractArchive(string resourceFileName)
{
try
{
string extractDir = System.IO.Path.GetTempPath();
extractDir = Path.Combine(extractDir, Guid.NewGuid().ToString());
Directory.CreateDirectory(extractDir);
string zipFilename = Path.Combine(extractDir, Guid.NewGuid().ToString());
CreateFileFromResource(resourceFileName, zipFilename);
ZipInputStream MyZipInputStream = null;
FileStream MyFileStream = null;
MyZipInputStream = new ZipInputStream(new FileStream(zipFilename, FileMode.Open, FileAccess.Read));
ZipEntry MyZipEntry = MyZipInputStream.GetNextEntry();
Directory.CreateDirectory(extractDir);
while (MyZipEntry != null)
{
if (MyZipEntry.IsDirectory)
{
Directory.CreateDirectory(extractDir + @"\" + MyZipEntry.Name);
}
else
{
if (!Directory.Exists(extractDir + @"\" + Path.GetDirectoryName(MyZipEntry.Name)))
{
Directory.CreateDirectory(extractDir + @"\" + Path.GetDirectoryName(MyZipEntry.Name));
}
MyFileStream = new FileStream(extractDir + @"\" + MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write);
int count;
byte[] buffer = new byte[4096];
count = MyZipInputStream.Read(buffer, 0, 4096);
while (count > 0)
{
MyFileStream.Write(buffer, 0, count);
count = MyZipInputStream.Read(buffer, 0, 4096);
}
MyFileStream.Close();
}
try
{
MyZipEntry = MyZipInputStream.GetNextEntry();
}
catch
{
MyZipEntry = null;
}
}
if (MyZipInputStream != null)
MyZipInputStream.Close();
if (MyFileStream != null)
MyFileStream.Close();
return extractDir;
}
catch { throw; }
}
示例2: Main
public static void Main(string[] args)
{
// Perform simple parameter checking.
if ( args.Length < 1 ) {
Console.WriteLine("Usage ViewZipFile NameOfFile");
return;
}
if ( !File.Exists(args[0]) ) {
Console.WriteLine("Cannot find file '{0}'", args[0]);
return;
}
// For IO there should be exception handling but in this case its been ommitted
byte[] data = new byte[4096];
using (ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]))) {
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {
Console.WriteLine("Name : {0}", theEntry.Name);
Console.WriteLine("Date : {0}", theEntry.DateTime);
Console.WriteLine("Size : (-1, if the size information is in the footer)");
Console.WriteLine(" Uncompressed : {0}", theEntry.Size);
Console.WriteLine(" Compressed : {0}", theEntry.CompressedSize);
if ( theEntry.IsFile ) {
// Assuming the contents are text may be ok depending on what you are doing
// here its fine as its shows how data can be read from a Zip archive.
Console.Write("Show entry text (y/n) ?");
if (Console.ReadLine() == "y") {
int size = s.Read(data, 0, data.Length);
while (size > 0) {
Console.Write(Encoding.ASCII.GetString(data, 0, size));
size = s.Read(data, 0, data.Length);
}
}
Console.WriteLine();
}
}
// Close can be ommitted as the using statement will do it automatically
// but leaving it here reminds you that is should be done.
s.Close();
}
}
示例3: Unzip
public static void Unzip(string file)
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(file))) {
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;
}
}
}
}
}
}
}
示例4: DecompressAndWriteFile
private static void DecompressAndWriteFile(string destination, ZipInputStream source)
{
FileStream wstream = null;
try
{
// create a stream to write the file to
wstream = File.Create(destination);
const int block = 2048; // number of bytes to decompress for each read from the source
var data = new byte[block]; // location to decompress the file to
// now decompress and write each block of data for the zip file entry
while (true)
{
int size = source.Read(data, 0, data.Length);
if (size > 0)
wstream.Write(data, 0, size);
else
break; // no more data
}
}
finally
{
if (wstream != null)
wstream.Close();
}
}
示例5: File
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipPath">源文件</param>
/// <param name="directory">目标文件</param>
/// <param name="password">密码</param>
public void File(string zipPath, string directory, string password = null) {
FileInfo objFile = new FileInfo(zipPath);
if (!objFile.Exists || !objFile.Extension.ToUpper().Equals(".ZIP")) return;
FileDirectory.DirectoryCreate(directory);
ZipInputStream objZIS = new ZipInputStream(System.IO.File.OpenRead(zipPath));
if (!password.IsNullEmpty()) objZIS.Password = password;
ZipEntry objEntry;
while ((objEntry = objZIS.GetNextEntry()) != null) {
string directoryName = Path.GetDirectoryName(objEntry.Name);
string fileName = Path.GetFileName(objEntry.Name);
if (directoryName != String.Empty) FileDirectory.DirectoryCreate(directory + directoryName);
if (fileName != String.Empty) {
FileStream streamWriter = System.IO.File.Create(Path.Combine(directory, objEntry.Name));
int size = 2048;
byte[] data = new byte[2048];
while (true) {
size = objZIS.Read(data, 0, data.Length);
if (size > 0) {
streamWriter.Write(data, 0, size);
} else {
break;
}
}
streamWriter.Close();
}
}
objZIS.Close();
}
示例6: ImportMethod
protected override void ImportMethod()
{
using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORT, STR_DOWNLOAD, 1, 0))
{
using (System.IO.TemporaryFile tmp = new System.IO.TemporaryFile(true))
using (System.Net.WebClient wc = new System.Net.WebClient())
{
wc.DownloadFile("http://www.globalcaching.eu/service/cachedistance.aspx?country=Netherlands&prefix=GC", tmp.Path);
using (var fs = System.IO.File.OpenRead(tmp.Path))
using (ZipInputStream s = new ZipInputStream(fs))
{
ZipEntry theEntry = s.GetNextEntry();
byte[] data = new byte[1024];
if (theEntry != null)
{
StringBuilder sb = new StringBuilder();
while (true)
{
int size = s.Read(data, 0, data.Length);
if (size > 0)
{
if (sb.Length == 0 && data[0] == 0xEF && size > 2)
{
sb.Append(System.Text.ASCIIEncoding.UTF8.GetString(data, 3, size - 3));
}
else
{
sb.Append(System.Text.ASCIIEncoding.UTF8.GetString(data, 0, size));
}
}
else
{
break;
}
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
XmlElement root = doc.DocumentElement;
XmlNodeList nl = root.SelectNodes("wp");
if (nl != null)
{
Core.Geocaches.AddCustomAttribute(CUSTOM_ATTRIBUTE);
foreach (XmlNode n in nl)
{
var gc = Utils.DataAccess.GetGeocache(Core.Geocaches, n.Attributes["code"].InnerText);
if (gc != null)
{
string dist = Utils.Conversion.StringToDouble(n.Attributes["dist"].InnerText).ToString("000.0");
gc.SetCustomAttribute(CUSTOM_ATTRIBUTE, dist);
}
}
}
}
}
}
}
}
示例7: Main
public static void Main(string[] args)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {
Console.WriteLine("Name : {0}", theEntry.Name);
Console.WriteLine("Date : {0}", theEntry.DateTime);
Console.WriteLine("Size : (-1, if the size information is in the footer)");
Console.WriteLine(" Uncompressed : {0}", theEntry.Size);
Console.WriteLine(" Compressed : {0}", theEntry.CompressedSize);
int size = 2048;
byte[] data = new byte[2048];
Console.Write("Show Entry (y/n) ?");
if (Console.ReadLine() == "y") {
// System.IO.Stream st = File.Create("G:\\a.tst");
while (true) {
size = s.Read(data, 0, data.Length);
// st.Write(data, 0, size);
if (size > 0) {
Console.Write(new ASCIIEncoding().GetString(data, 0, size));
} else {
break;
}
}
// st.Close();
}
Console.WriteLine();
}
s.Close();
}
示例8: RestoreFromFile
private void RestoreFromFile(Stream file)
{
//
//
using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
using (var zip = new ZipInputStream(file)) {
try {
while (true) {
var ze = zip.GetNextEntry();
if (ze == null) break;
using (var f = new IsolatedStorageFileStream(ze.Name, FileMode.Create, store)) {
var fs = new byte[ze.Size];
zip.Read(fs, 0, fs.Length);
f.Write(fs, 0, fs.Length);
}
}
} catch {
lblLastBackup.Text = StringResources.BackupAndRestorePage_Messages_RestoreFailed;
App.ToastMe(StringResources.BackupAndRestorePage_Messages_RestoreFailed);
return;
} finally {
file.Close();
ClearOldBackupFiles();
App.ViewModel.IsRvDataChanged = true;
}
}
}
lblLastBackup.Text = StringResources.BackupAndRestorePage_Messages_RestoreSuccess;
App.ToastMe(StringResources.BackupAndRestorePage_Messages_RestoreSuccess);
}
示例9: UnZip
public static void UnZip(byte[] inputZipBinary, string destinationPath)
{
DirectoryInfo outDirInfo = new DirectoryInfo(destinationPath);
if (!outDirInfo.Exists)
outDirInfo.Create();
using (MemoryStream msZipBinary = new MemoryStream(inputZipBinary))
{
using (ZipInputStream zipFile = new ZipInputStream(msZipBinary))
{
ZipEntry zipEntry;
while ((zipEntry = zipFile.GetNextEntry()) != null)
{
FileStream fsOut = File.Create(outDirInfo.FullName + "\\" + zipEntry.Name);
byte[] buffer = new byte[4096]; int count = 0;
#if DEBUG
Console.WriteLine("Descomprimiendo: " + zipEntry.Name +
" |Tamaño comprimido: " + zipEntry.CompressedSize +
" |Tamano descomprimido: " + zipEntry.Size +
" |CRC: " + zipEntry.Crc);
#endif
while ((count = zipFile.Read(buffer, 0, buffer.Length)) > 0)
fsOut.Write(buffer, 0, count);
fsOut.Flush();
fsOut.Close();
}
}
}
}
示例10: UncompressContent
public static string UncompressContent(byte[] zippedContent)
{
try
{
MemoryStream inp = new MemoryStream(zippedContent);
ZipInputStream zipin = new ZipInputStream(inp);
ZipEntry entryin = zipin.GetNextEntry();
byte[] buffout = new byte[(int)zipin.Length];
zipin.Read(buffout, 0, (int)zipin.Length);
MemoryStream decompress = new MemoryStream(buffout);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string result = enc.GetString(decompress.ToArray());
decompress.Dispose();
inp.Dispose();
return result;
}
catch (Exception ex)
{
ex.Message.ToString();
return null;
}
}
示例11: Decompress
public bool Decompress()
{
Boolean gotIt = false;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsFile && theEntry.Name == m_msi)
{
gotIt = true;
FileStream streamWriter = File.Create(msiFilePath);
long filesize = theEntry.Size;
byte[] data = new byte[filesize];
while (true)
{
filesize = s.Read(data, 0, data.Length);
if (filesize > 0)
{
streamWriter.Write(data, 0, (int)filesize);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
return gotIt;
}
示例12: Estrai
public string[] Estrai(string file)
{
string[] ret = null;
int newsize = 0;
if (!File.Exists(file))
{
Console.WriteLine("Cannot find file '{0}'", file);
return null;
}
string directoryName = Path.GetDirectoryName(file);
using (ZipInputStream s = new ZipInputStream(File.OpenRead(file)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
newsize++;
Console.WriteLine(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(System.IO.Path.Combine(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;
}
}
}
}
string[] tmp = new string[newsize];
tmp[newsize - 1] = System.IO.Path.Combine(directoryName, fileName);
if (ret != null)
System.Array.Copy(ret, tmp,
System.Math.Min(ret.Length, tmp.Length));
ret = tmp;
}
}
return ret;
}
示例13: ExtractZipEntry
private static void ExtractZipEntry(
string destinationRootFolderPath,
ZipInputStream zipStream,
ZipEntry theEntry)
{
string fileName = Path.GetFileName(theEntry.Name);
if (string.IsNullOrEmpty(fileName))
return;
string directoryName = Path.GetDirectoryName(theEntry.Name);
if (string.IsNullOrEmpty(directoryName))
return;
// create directory
string destinationFolderPath =
Path.Combine(
destinationRootFolderPath,
directoryName);
Directory.CreateDirectory(destinationFolderPath);
string destinationFilePath = Path.Combine(destinationFolderPath, fileName);
using (FileStream streamWriter = File.Create(destinationFilePath))
{
var data = new byte[2048];
while (true)
{
int size = zipStream.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
}
}
示例14: UnzipText
public static string UnzipText(byte[] zippedText)
{
if (zippedText == null || zippedText.Length == 0) return "";
StringBuilder sb = new StringBuilder();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(zippedText))
using (ZipInputStream s = new ZipInputStream(ms))
{
ZipEntry theEntry = s.GetNextEntry();
byte[] data = new byte[1024];
if (theEntry != null)
{
while (true)
{
int size = s.Read(data, 0, data.Length);
if (size > 0)
{
sb.Append(System.Text.UTF8Encoding.UTF8.GetString(data, 0, size));
}
else
{
break;
}
}
}
}
return sb.ToString();
}
示例15: ZipInputStream
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="输入压缩文件路径">要解压的压缩文件</param>
/// <param name="解压缩目录">解压目标目录路径</param>
public static void 解压缩(string 输入压缩文件路径, string 解压缩目录)
{
ZipInputStream inputStream = new ZipInputStream(File.OpenRead(输入压缩文件路径));
ZipEntry theEntry;
while ((theEntry = inputStream.GetNextEntry()) != null)
{
解压缩目录 += "/";
string fileName = Path.GetFileName(theEntry.Name);
string path = Path.GetDirectoryName(解压缩目录 + theEntry.Name) + "/";
Directory.CreateDirectory(path);//生成解压目录
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + fileName);//解压文件到指定的目录
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = inputStream.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
inputStream.Close();
}