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.
Related
I am using the following Script Task to enumerate over a file directory, then pull a list of files and load them into a Recordset Destination. I don't want to drill down past the directory I specified #"E:\Data Warehouse\Imports\TIMECLOCK" in my search. There's a subsequent folder inside TIMECLOCK called PROCESSED, where it's supposed to move the files into later. So I'm only trying to grab the ones in the folder above, TIMECLOCK. So I used the SearchOption.TopDirectoryOnly in the Directory.GetFiles function, but it's drilling down past that to the PROCESSED folder, as evidenced by my Data Viewer (see Data View screenshot after the code). At first I was using a variable to pass in the search directory, but it was doing the same thing, so I explicitly declared the search directory, and it's still not working. I don't know what I'm doing wrong!
#region Namespaces
using System;
using System.Data;
using System.IO;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
#endregion
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent{
public override void CreateNewOutputRows()
{
string sourceDirectory = #"E:\Data Warehouse\Imports\TIMECLOCK";
string searchFileName = "Business Units GL Segments_Sent Weekly*";
string[] files = Directory.GetFiles(sourceDirectory, searchFileName, SearchOption.TopDirectoryOnly);
FileInfo fileInfo = new FileInfo(sourceDirectory);
foreach (string file in files)
{
String FileFullPath = Path.GetFullPath(file);
fileInfo = new FileInfo(FileFullPath);
OrderedFilesBuffer.AddRow();
OrderedFilesBuffer.FileName = fileInfo.Name;
OrderedFilesBuffer.FileDate = fileInfo.LastWriteTime;
OrderedFilesBuffer.FilePath = fileInfo.FullName;
}
}
}
I am marking as answered because I am still completely not sure why this was happening, and I ultimately decided what I was trying to reproduce with writing the values to (2) file objects, then merging and getting a unique list of files between them to process, was really just a simple File Move task. I was writing this for a client who originally designed it in Pentaho Data Integration that way and was reproducing it exactly in SSIS, when it was not really necessary. I think since I was running both of these extractions simultaneously in the same Control Flow, it was using the same file path for both because of a cache issue of some sort and ignoring the TopDirectoryOnly. At any rate, I trashed it and re-did the whole thing.
I want to create content of a zip file in treeview, but my problem is how to recognize the content of zip file is file or directory without extract the files and then add them to tree?
would you mind help me in solving this?
This functionality is available out of the box in .Net Framework 4.5 and later. You have to use this library:
using System.IO.Compression;
And then you'll be able to do this:
string zipPath = #"c:\example\start.zip";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith('\'))
Console.WriteLine($"{entry.FullName} is a directory.");
else
Console.WriteLine($"{entry.FullName} is a file.");
}
}
See here for more details:
How to list the contents of a .zip folder in c#?
I'm trying to zip several folders with their content into one zip file using Ionic Zip Library. The code below did created a zip file, however the folders were not added to it.
private void ZipFolder(List <string> folders, string pathToSaveZipFile)
{
using (ZipFile zip = new ZipFile())
{
foreach (string itrFolder in folders)
{
zip.AddDirectory(itrFolder);
}
zip.Save(pathToSaveZipFile);
}
}
Thank you
AddDirectory(string) adds the files to the root, you need to use
zip.AddDirectory(itrFolder, new DirectoryInfo(itrFolder).Name);
The second argument specifies the folder name within the ZIP.
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.
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.