How to unzip docx file using C#? - c#

How to unzip docx file using C#?

The new Office file extensions (docx,potx,xlsx,etc) turning into zip files when they are uploaded to a web server and then downloaded.
These file formats are now using an Open XML file format system so they are more compatible with other office programs from Google, Open Office...etc. Essentially they are zip files that are full of XML files that when opened with a proper application turn into a friendly word document.
I stole this full of shame from here where you can find full info.
I hope this answer will help you and all the ignorant people that made fun of you and negative voting your question without even knowing the answer.

If you mean docx files, they're basically just zip files created with a particular convention.
Look into the Packaging API.

Here is the complete code that you are looking for. I have 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);
}
}
}

Set reference 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)

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

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

Related

How can I move a downloaded file to the users temp path?

I've looked it up on google, tried any answers for myself and none worked. I want to download 2 files and save them both to the users temp file (C:\Users%UserName%\AppData\Local\Temp). I can easily find the temp file using a string (string tempPath = Environment.GetEnvironmentVariable("TEMP");) I just can't get farther than that at this time.
You can download directly into the temp folder:
using System.Net;
string tempPath = Environment.GetEnvironmentVariable("TEMP");
using (var client = new WebClient())
{
client.DownloadFile("http://example.com/file/file1.txt", #$"{tempPath}\file.txt");
client.DownloadFile("http://example.com/file/file2.txt", #$"{tempPath}\file2.txt");
}
Or move the already downloaded file:
using System.IO;
string tempPath = Environment.GetEnvironmentVariable("TEMP");
File.Move(#"C:\User\Downloads\Filename.txt", #$"{tempPath}\file.txt");
File.Move(#"C:\User\Downloads\Filename2.txt", #$"{tempPath}\file2.txt");
The # before the string is the string literal and The $ is the string interpolation you can search for these nice features later.
Adding to the accepted answer:
you can also derive the download-folder path to work on machine which use a different download folder as the default. You can find input on that here: How to programmatically derive Windows Downloads folder "%USERPROFILE%/Downloads"?
So if you chose to implement the second solution (moving files already downloaded) and need to roll this out on other machines than your own, this probably is a good idea.

How to get the files from the hierarchy of Zip Folder

I have a directory Structure like this:
A.zip -
A -
a -
1.dat
2.dat
I want to read the files 1.dat and 2.dat inside the directory hierarchy. I am able to read the file contentby C#, if the file is directly inside the zip folder but due to the inner directory structure is become inaccessible.
Any Help would be appreciated.
Thanks in advance.
Not sure how you are reading your zip file contents without an example, however reading zip file contents using the System.IO.Compression and System.IO.Compression.FileSystem assemblies is pretty simplistic. See the following example of how to read all files regardless of subdirectory within a zip file:
using System;
using System.IO.Compression;
namespace ZipReader
{
class Program
{
const string zipPath = #"D:\test\test.zip";
static void Main(string[] args)
{
using (var archive = ZipFile.OpenRead(zipPath))
{
foreach (var entry in archive.Entries)
{
Console.WriteLine(entry.FullName);
}
}
Console.ReadKey();
}
}
}
Produces the following output:
folder1/test1.txt
folder2/test2.txt
To get the contents you can simply call entry.Open() on each file which returns a Stream you can handle however you need to.

c# opening an embedded file (.doc, .pdf, .exe etc.)

I am new to C# and I have normally built windows forms using VB and was able to use one code to open any embedded file I added to my "Resources". As far as C# I have looked online for hours and have yet to find anything that worked. Please assist in any way that you can.
I have a Windows Form that will have a single button that will be assigned to open a particular file I have added to the "Resources" folder. Usually I would use the following code to have a Button_Click to load an exe, doc or pdfile. I am looking for something similar for C#.
VB Code:
IO.File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Temp & "\IEResetConfigure.exe", My.Resources.IEResetConfigure)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\IEResetConfigure.exe")
Simply write your resource file to temporary directory and run the file
using System;
using System.IO;
using System.Diagnostics;
// ...
byte[] resourceFile = Properties.Resources.Newspaper_PC_13_12_2013;
string destination = Path.Combine(Path.GetTempPath(), "Newspaper_PC_13_12_2013.pdf");
System.IO.File.WriteAllBytes(destination, resourceFile);
Process.Start(destination);
Example of my comment
using System;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;
// ...
static void Main(string[] args)
{
byte[] resourceFile = Properties.Resources.Newspaper_PC_13_12_2013;
string destination = Path.Combine(Path.GetTempPath(), "Newspaper_PC_13_12_2013.pdf");
File.WriteAllBytes(destination, resourceFile);
Process.Start(destination);
AutoDelete(2000, destination);
Console.Write("Press any key to quit . . . ");
Console.ReadKey(true);
}
static async void AutoDelete(int milliseconds, string destination)
{
while (File.Exists(destination))
{
await Task.Delay(milliseconds);
try
{
File.Delete(destination);
}
catch
{
continue;
}
}
}
For anyone still looking, Here is a way of opening an "embedded" file. I'd love for someone to correct me below on a better way.
The first part is to make sure your file is added to your project in the bin\debug folder.
I then used this code to call it
private void button1_Click(object sender, EventArgs e)
{
//Place file in .\bin\Debug folder of project
string filename = "YourFileName.pdf";
System.Diagnostics.Process.Start(filename);
For full Disclosure this whole part has been stolen from
(Opening a .pdf file in windows form through a button click)
I did however run into an issue where after the first build that didn't work for me. So when I created the setup project I added the "Project Output" and then added in my pdf via "add file" to the application folder.
That has continued to work flawlessly for me since.
This is my first post on Stack Overflow, so please let me know if I misunderstood any rules or could improve. Thank you and I hope this helped!

Cannot read that as a ZipFile for .exe file C#

I'm using SevenZIP library files to unzip/extract .exe file. When i tried this approcah I'm getting a error Cannot read that as a ZipFile & zip exception was unhanded. I don't want to use any 7zip.exe console app in my project & i prefer to use .dll files in my project.
Is there any other way to extract .exe file?
private void MyExtract()
{
if(x86)
ExtractZip(#"D:\22.1.2.702\64\953-win_x86.exe", ".");
else
ExtractZip(#"D:\22.1.2.702\64\.702-win_x64.exe", ".");
}
private void ExtractZip(string zipFile, string directory)
{
using (var zip1 = ZipFile.Read(zipFile))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (var e in zip1)
{
e.Extract(directory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
Code sample looks like you are using DotNetZip and not SevenZipLib. DotNetZip can only extract .zip files, not 7-zip nor .exe.
Instead of using SevenZip lib, try 7zip.exe in console. Use Process class to execute 7zip.exe. It works perfect.

How Can I Check That A File Is A Valid XPS File With C#?

I have a WinForms application that processes XPS files. How can I check that the file the user has selected in the open dialog is a valid XPS file using C#?
There WILL be files present with the .XPS extension that are not really XPS files.
Since XPS files are really in the PKZIP format, I could check for the PKZIP byte signature but that would give false positives on ZIP archives.
The following will distinguish XPS files from other ZIP archives and non-ZIP files. It won't determine whether the file is fully-valid XPS - for that you would need to load each page.
using System;
using System.IO;
using System.Windows.Xps.Packaging;
class Tester
{
public static bool IsXps(string filename)
{
try
{
XpsDocument x = new XpsDocument(filename, FileAccess.Read);
IXpsFixedDocumentSequenceReader fdsr = x.FixedDocumentSequenceReader;
// Needed to actually try to find the FixedDocumentSequence
Uri uri = fdsr.Uri;
return true;
}
catch (Exception)
{
}
return false;
}
}
You can check for the content Type of the file instead of the file extension.

Categories

Resources