[英]How to unzip docx file using C#?
如何使用C#解壓縮docx文件?
如果您指的是docx
文件,則它們基本上只是按照特定約定創建的zip
文件。
查看包裝 API。
這是您正在尋找的完整代碼。 我已經將此類用於docx zip和unzip操作。
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Deployment.Compression;
using Microsoft.Deployment.Compression.Zip;
namespace <YourPackage>.Libs
{
public class ZipFile
{
private string _zipfilepath;
public ZipFile(string zipfilepath)
{
_zipfilepath = zipfilepath;
}
public void Compress(string filePath,bool deleteSourceFolder)
{
var filePaths = new List<string>();
if (Directory.Exists(filePath))
{
filePaths.AddRange(Directory.GetFileSystemEntries(filePath).ToList());
}
if (filePaths.Count > 0)
{
var zip = new ZipInfo(_zipfilepath);
zip.Pack(filePath, true, CompressionLevel.None, null);
}
if(deleteSourceFolder)
Directory.Delete(filePath,deleteSourceFolder);
}
public void Uncompress(string destinationPath)
{
var zip = new ZipInfo(_zipfilepath);
zip.Unpack(destinationPath);
}
}
}
設置對System.IO.Compression和System.IO.Compression.FileSystem的引用。 然后是這樣的:
using System.IO.Compression;
string zipPath = @"c:\tmp\Test.docx";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
archive.ExtractToDirectory(zipPath + ".unzipped");
}
在這里看看: https ://msdn.microsoft.com/ZH-US/library/hh485709(v = VS.110,d = hv.2).aspx(ZipFileExtensions.ExtractToDirectory方法)
您可以嘗試使用System.IO.Packaging.ZipPackage 。
安裝Open XML SDK http://www.microsoft.com/zh-cn/download/details.aspx?id=5124並使用它來處理Docx文件中的XML。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.