I need to zip a file in the folder c:\projects\test\test.log
I need to zip the test.log to a zip folder.I am using Ionic.zip.dll.
when I zipped the folder, it is entirely adding the folder c:\projects\test\test.log.
I need to add the test.log to a zip folder
for eg.jan.zip. it should contain only one file. no folder structure needed.
Thanks
Instead of
zipFile.AddFile(filename);
you should use
zipFile.AddFile(filename, "");
That will insert the file at the root of the zip file, ie without a path.
Related
I want to add(and overwrite) all files including other folders that are in single folder.
Example:
In archive zip.zip:
work.docx
1.txt
Photos
|_ocean.png
|_horse.png
After adding files & folder from another folder it would be like:
work.docx
1.txt
code.cs
Photos
|_ocean.png
|_horse.png
Program
|_program.exe
|_config.txt
I tried to do that with Ionic's zip library:
Ionic.Zip.ZipFile zf = Ionic.Zip.ZipFile.Read(#"C:\\zip.zip");
zf.UpdateDirectory(#"C:\\Program");
zf.Save();
It works, but it doesn't overwrite files.
If you are using .NET 4.5 then you can use
System.IO.Compression.ZipArchive
It allows a single step compression.
ZipFile.CreateFromDirectory(#”C:\zip”, #”C:\zip.zip”);
To update it you can use ZipArchiveMode.Update
I am trying to zip a folder with all its files and folders but its not working properly.
Here is the Folder i am trying to Zip it contains some files and a subfolder
and here is how i am doing it.
System.IO.Compression.ZipFile.CreateFromDirectory(DestPath, ZipPath,System.IO.Compression.CompressionLevel.Optimal,true);
but the result is this
It does not include xml file and the subfolder. Expected output should be like this
Is it possible to achieve without using an external library ?
I want to zip some files in a directory. I have googled a lot but i always saw examples about zipping a directory (all files in directory) by the code below:
string startPath = #subPath;
string zipPath = #"C:\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
I am referencing System.IO.Compression.Filesystem.dll
This code zips all the files in the directory referenced by startPath.
I tried ZipArchive class for compressing but it compress file with directories on its path. For example,
assume i want to zip "a.txt" located at "C:\directories\Graphics\a.txt". The zip file contains directories "directories" and "Graphics" and into these directories the file "a.txt". It is not a good solution to cut files then erase the directories and paste again since i am working on big size data.
How can I zip only some of files at a directory?
How can I zip some files that are located in different directories?
I am looking up the subject for 2 days. Is it a kind of constraint in .Net?
You can use the ZipArchive class, first create it and then - once created you just add ZipArchiveEntry objects into the archive. Each of the ZipArchiveEntry objects represents a single file that you will add to the archive.
Some tips:
You can find more info and a sample of how this could be done on MSDN, here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchive(v=vs.110).aspx and here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchiveentry(v=vs.110).aspx.
This may not be an exact solution but should work for your scenario. I'll give you the steps :
Create a Temporary Directory
Read and copy your Selected files into the Temporary directory. This applies to your second scenario as well.
Zip the temporary directory
Delete Temporary Directory after you are done
I would recommend using File.Copy to copy all your desired files to a temporary directory, then zip them all from there and delete the temporary directory once the zip operation has finished.
I've two zip files in a folder
C:\WorkUnit\Network\Reports\
Zip files:
LatestReport-20140312_202854-NWTS-a345sfsd3353.zip
LatestReport-20140312_202854-NITS SPACE-d56shj67d3gt8p.zip
In each zip file i've few folders and log files
Lets take first zip file: LatestReport-20140312_202854-NWTS-a345sfsd3353.zip
Within the zip file and within the subFolders, text files are there.
CurrentStack\Unit\NWTS\
Service.txt
SpaceA.txt
SpaceB.txt
I need to check these text files at the above location is exists or not at this path "C:\Collection\LatestFiles\"
How can i check the textfiles existence(no content check)?
My problem is that I want zip my file into zip formatted file. I used DotNetZib library works fine but there is a little problem in there. I read file from C:\Users\Hüseyin\Desktop\AA for instance. But it creates full path which read from path given above in zip file.I want that when i clicked the zip file a.txt will be directly...Is there anyone to help me?
using (ZipFile zip=new ZipFile())
{
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
zip.AddItem(#"C:\Users\Hüseyin\Desktop\AA\a.txt");
zip.Save(#"C:\Users\Hüseyin\Desktop\AA\deneme.zip");
}
This code block uses Ionic.Zip; dll and in zip file all path is there i want only a.txt will be in deneme.zip without any directions.Is this more useful to understand.If i wrote wrong sorry, my english is not very good.
the ZipFile.AddItem method have another variant with two strings source file path and file path in archive.
You have to use :
string filepath = #"C:\Users\Hüseyin\Desktop\AA\a.txt";
zip.AddItem(filePath, Path.GetFileName(filePath));
to put the a.txt file in the root directory of the zip file.