SharZipLib压缩解压及获取Excel2007中页眉眉脚图片
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using System.Xml;
namespace SharpZipLibTest
{
public class Program
{
static void Main()
{
//CreateZipFile(@"d:\", @"d:\a.zip");
UnZipFile(@"d:\a.zip");
string rootPath = Path.Combine(Application.StartupPath, "Data");
string templateFile = Path.Combine(rootPath, "Template.xlsx");
string zipFile = templateFile.Substring(0, templateFile.LastIndexOf(".")) + ".zip";
File.Copy(templateFile, zipFile);//convert xlsx file to zip file
UnZipFile(templateFile, Path.Combine(rootPath, "unZipped"));
Hashtable ht = new Hashtable();
GetFilePath(ref ht);
#region output header and footer's image path from xml
if (ht["LH"] != null)
Console.WriteLine("LH:{0}", ht["LH"].ToString());
if (ht["CH"] != null)
Console.WriteLine("CH:{0}", ht["CH"].ToString());
if (ht["RH"] != null)
Console.WriteLine("RH:{0}", ht["RH"].ToString());
if (ht["LF"] != null)
Console.WriteLine("LF:{0}", ht["LF"].ToString());
if (ht["CF"] != null)
Console.WriteLine("CF:{0}", ht["CF"].ToString());
if (ht["RF"] != null)
Console.WriteLine("RF:{0}", ht["RF"].ToString());
#endregion
}
private static void CreateZipFile(string filesPath, string zipFilePath)
{
if (!Directory.Exists(filesPath))
{
Console.WriteLine("Cannot find directory '{0}'", filesPath);
return;
}
try
{
string[] filenames = Directory.GetFiles(filesPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
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();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
}
#region 从XML中解析页眉页脚中图片
private static void GetFilePath(ref Hashtable ht)
{
string vmlDrawing1 = Application.StartupPath + "\\Data\\unZipped\\xl\\drawings\\vmlDrawing1.vml";
string vmlDrawing1_rels = Application.StartupPath + "\\Data\\unZipped\\xl\\drawings\\_rels\\vmlDrawing1.vml.rels";
string image_which = string.Empty;
string image_key = string.Empty;
string image_path = string.Empty;
XmlDocument root = new XmlDocument();
root.Load(vmlDrawing1);
XmlDocument root_rels = new XmlDocument();
root.Load(vmlDrawing1_rels);
XmlNodeList xnl = root.GetElementsByTagName("v:shape");
foreach (XmlNode xn in xnl)
{
image_which = xn.Attributes["Id"].Value;
foreach (XmlNode node in xn.ChildNodes)
{
image_key = node.Attributes["o:relid"].Value;
break;
}
XmlNodeList xnl_rels = root_rels.GetElementsByTagName("Relationship");
foreach (XmlNode xn_rels in xnl_rels)
{
if (image_key == xn_rels.Attributes["Id"].Value)
{
image_path = xn_rels.Attributes["Target"].Value;
ht.Add(image_key, image_path);
break;
}
}
}
}
#endregion
#region 解压文件到当前目录
private static void UnZipFile(string zipFilePath)
{
if (!File.Exists(zipFilePath))
{
Console.WriteLine("Cannot find file '{0}'", zipFilePath);
return;
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
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();
}
}
}
s.Close();
}
}
#endregion
#region 解压文件至指定目录
private static bool UnZipFile(string sourceFile, string destinationFile)
{
bool ret = true;
try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFile)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(destinationFile);
string fileName = Path.GetFileName(theEntry.Name);
if (fileName != String.Empty)
{
//如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
if (theEntry.CompressedSize == 0)
break;
//解压文件到指定的目录
directoryName = Path.GetDirectoryName(destinationFile + theEntry.Name);
//建立下面的目录和子目录
Directory.CreateDirectory(directoryName);
using (FileStream streamWriter = File.Create(destinationFile + 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();
}
}
catch (ZipException ze)
{
ret = false;
throw ze;
}
return ret;
}
#endregion
}
}
浙公网安备 33010602011771号