如何使用C#解压缩docx文件?

如何使用C#解压缩docx文件?

新的Office文件扩展名(docx,potx,xlsx等)在上传到Web服务器然后下载时变成zip文件。

这些文件格式现在使用Open XML文件格式系统,因此它们与Google,Open Office等其他办公程序更兼容。 基本上它们是包含XML文件的zip文件,当使用适当的应用程序打开时,它们变成友好的word文档。

我从这里偷走了这个充满耻辱的地方, 在那里你可以找到完整的信息。

我希望这个答案可以帮助你和所有嘲笑你的无知的人,并在不知道答案的情况下对你的问题进行否定投票。

如果你的意思是docx文件,它们基本上只是用特定约定创建的zip文件。

查看Packaging API。

这是您正在寻找的完整代码。 我已经将这个类用于docx zip和unzip操作。

  using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.Deployment.Compression; using Microsoft.Deployment.Compression.Zip; namespace .Libs { public class ZipFile { private string _zipfilepath; public ZipFile(string zipfilepath) { _zipfilepath = zipfilepath; } public void Compress(string filePath,bool deleteSourceFolder) { var filePaths = new List(); 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/EN-US/library/hh485709(v = VS.110,d = hv.2).aspx(ZipFileExtensions.ExtractToDirectory Method)

您可以尝试使用System.IO.Packaging.ZipPackage 。