How to unzip a docx file using C #?

How to unzip a docx file using C #?

+4
source share
6 answers

New Office file extensions (docx, potx, xlsx, etc.) turn into zip files when they are uploaded to the web server and then uploaded.

These file formats now use the Open XML format system, so they are more compatible with other office programs from Google, Open Office ... etc. In fact, they are zip files that are filled with XML files, which when opened with the corresponding application turn into a friendly word document.

I stole this shame from here , where you can find complete information.

I hope this answer helps you and all ignorant people who ridiculed you and negatively voted on your question, without even knowing the answer.

+5
source

If you mean docx files, they are basically just zip files created with a specific convention.

Take a look at the Packaging API.

+4
source

Here is the complete code you are looking for. I used this class for docx zip and unzip operations.

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

}

+1
source

Install a link to System.IO.Compression and System.IO.Compression.FileSystem. Then something like this:

 using System.IO.Compression; string zipPath = @"c:\tmp\Test.docx"; using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { archive.ExtractToDirectory(zipPath + ".unzipped"); } 

Have a look here: https://msdn.microsoft.com/EN-US/library/hh485709(v=VS.110,d=hv.2).aspx (ZipFileExtensions.ExtractToDirectory method)

+1
source

You can try using System.IO.Packaging.ZipPackage .

0
source

Install the Open XML SDK http://www.microsoft.com/en-us/download/details.aspx?id=5124 and use it to work with XML inside Docx files.

0
source

All Articles