2013-03-06 12 views

回答

4

新的Office文件擴展名(DOCX,POTX,XLSX等)轉變成zip文件時,他們被上傳到Web服務器,然後下載。

這些文件格式現在使用Open XML文件格式系統,因此它們與Google,Open Office等其他辦公軟件更兼容。從本質上講,它們是包含多個XML文件的zip文件,這些文件在用適當的應用程序打開時變成友好的word文檔。

我偷了這個充滿恥辱的從here在這裏你可以找到完整的信息。

我希望這個答案將幫助您和所有由你和消極投票你的問題,甚至不知道答案樂趣無知的人。

+0

雖然這提供了很好的信息,但是您引用的博客文章更多地討論了Web服務器在2007年無法爲基於Open XML的Office文檔提供正確的MIME類型:)要正​​確使用這些文件並且不要重新發明輪盤,請打開「打開XML SDK是要走的路。 – 2013-03-06 08:13:14

+0

@David Khaykin你是對的,但我只是想給他一個提示,確實這些文件可能是zip文件。 – Dummy01 2013-03-06 08:44:34

+0

@ Dummy01非常感謝你:) – 2013-03-06 09:14:17

4

如果你的意思是docx文件,他們基本上只是zip文件創建一個特定的約定。

查看Packaging API。

1

這是您正在尋找的完整代碼。我已經使用這個類來進行docx zip和解壓縮操作。

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); 
    }  
} 

}

+0

你從哪裏得到Microsoft.Deployment命名空間?我有所有的默認引用,但是Microsoft沒有一個部署命名空間。 – HadesHerald 2015-08-12 22:06:27

+0

對不起,遲到的迴應。您必須安裝[Wix](http://wixtoolset.org/)。如果您正在修改word文檔,我可以強烈推薦[DocX](https://docx.codeplex.com/)。 – 2015-11-10 04:38:03

1

設置參考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法)