ZipOutputStream
फ़ोल्डर में केवल ज़िप फ़ाइलों का उपयोग करते हुए ज़िप उपफोल्डर। मैं subfolders भी ज़िप करना चाहता हूँ। इसे कैसे प्राप्त किया जा सकता है?ज़िप उपफोल्डर्स ZipOutputStream
उत्तर
आपको ज़िप में सभी फ़ाइलों को जोड़ने के लिए अपनी निर्देशिका का पुन: प्रयास करना होगा।
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
namespace Zip
{
/// <summary>
/// Uses Sharpziplib so as to create a non flat zip archive
/// </summary>
public abstract class ZipManager
{
/// <summary>
/// will zip directory .\toto as .\toto.zip
/// </summary>
/// <param name="stDirToZip"></param>
/// <returns></returns>
public static string CreateZip(string stDirToZip)
{
try
{
DirectoryInfo di = new DirectoryInfo(stDirToZip);
string stZipPath = di.Parent.FullName + "\\" + di.Name + ".zip";
CreateZip(stZipPath, stDirToZip);
return stZipPath;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Main method
/// </summary>
/// <param name="stZipPath">path of the archive wanted</param>
/// <param name="stDirToZip">path of the directory we want to create, without ending backslash</param>
public static void CreateZip(string stZipPath, string stDirToZip)
{
try
{
//Sanitize inputs
stDirToZip = Path.GetFullPath(stDirToZip);
stZipPath = Path.GetFullPath(stZipPath);
Console.WriteLine("Zip directory " + stDirToZip);
//Recursively parse the directory to zip
Stack<FileInfo> stackFiles = DirExplore(stDirToZip);
ZipOutputStream zipOutput = null;
if (File.Exists(stZipPath))
File.Delete(stZipPath);
Crc32 crc = new Crc32();
zipOutput = new ZipOutputStream(File.Create(stZipPath));
zipOutput.SetLevel(6); // 0 - store only to 9 - means best compression
Console.WriteLine(stackFiles.Count + " files to zip.\n");
int index = 0;
foreach (FileInfo fi in stackFiles)
{
++index;
int percent = (int)((float)index/((float)stackFiles.Count/100));
if (percent % 1 == 0)
{
Console.CursorLeft = 0;
Console.Write(_stSchon[index % _stSchon.Length].ToString() + " " + percent + "% done.");
}
FileStream fs = File.OpenRead(fi.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//Create the right arborescence within the archive
string stFileName = fi.FullName.Remove(0, stDirToZip.Length + 1);
ZipEntry entry = new ZipEntry(stFileName);
entry.DateTime = DateTime.Now;
// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipOutput.PutNextEntry(entry);
zipOutput.Write(buffer, 0, buffer.Length);
}
zipOutput.Finish();
zipOutput.Close();
zipOutput = null;
}
catch (Exception)
{
throw;
}
}
static private Stack<FileInfo> DirExplore(string stSrcDirPath)
{
try
{
Stack<DirectoryInfo> stackDirs = new Stack<DirectoryInfo>();
Stack<FileInfo> stackPaths = new Stack<FileInfo>();
DirectoryInfo dd = new DirectoryInfo(Path.GetFullPath(stSrcDirPath));
stackDirs.Push(dd);
while (stackDirs.Count > 0)
{
DirectoryInfo currentDir = (DirectoryInfo)stackDirs.Pop();
try
{
//Process .\files
foreach (FileInfo fileInfo in currentDir.GetFiles())
{
stackPaths.Push(fileInfo);
}
//Process Subdirectories
foreach (DirectoryInfo diNext in currentDir.GetDirectories())
stackDirs.Push(diNext);
}
catch (Exception)
{//Might be a system directory
}
}
return stackPaths;
}
catch (Exception)
{
throw;
}
}
private static char[] _stSchon = new char[] { '-', '\\', '|', '/' };
}
}
यह सी # एमवीपी पीटर Bromberg द्वारा लेख आपकी समस्या का समाधान हो सकता है:: Zip/Unzip folders and files with C#
इस छोटे सहायक यदि आप चाहें तो देखें। इसमें पूरा कोड और नमूना प्रोजेक्ट है।
वैकल्पिक (और अधिक काम) समाधान मैं बस पर ठोकर खाई:
SevenZipSharp परियोजना
var compressor = new SevenZipCompressor();
compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
compressor.CompressionLevel = CompressionLevel.High;
compressor.CompressionMode = CompressionMode.Create;
compressor.PreserveDirectoryRoot = false;
compressor.FastCompression = true;
compressor.CompressDirectory(dir.FullName, zipFile.FullName);
public static void ZipDirectoryKeepRelativeSubfolder(string zipFilePath, string directoryToZip)
{
var filenames = Directory.GetFiles(directoryToZip, "*.*", SearchOption.AllDirectories);
using (var s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);// 0 - store only to 9 - means best compression
var buffer = new byte[4096];
foreach (var file in filenames)
{
var relativePath = file.Substring(directoryToZip.Length).TrimStart('\\');
var entry = new ZipEntry(relativePath);
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (var fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
ने मेरे लिए काम किया है, धन्यवाद – mrbm
फिनिश() और क्लोज़() कार्यों के साथ जिस तरह से निपटान पैटर्न के साथ नहीं है। :( –
- 1. एक फ़ोल्डर को ज़िप करना जिसमें उपफोल्डर्स
- 2. समापन ZipOutputStream
- 3. उप-निर्देशिकाएं ZipOutputStream
- 4. ब्राउज़र पर रीपिंग ZipOutputStream
- 5. स्ट्रीम ज़िप फ़ाइल
- 6. उपफोल्डर्स से पाइथन यादृच्छिक रेखाएं
- 7. उपफोल्डर्स के माध्यम से लूप
- 8. उपफोल्डर्स के तहत एंड्रॉइड लेआउट
- 9. जावा में ज़िप या निकालने के दौरान ज़िप उपयोगिता में प्रगति पट्टी को कैसे जोड़ना है?
- 10. ज़िप
- 11. ज़िप
- 12. Windows में ज़िप फ़ाइल बनाएँ और ज़िप फ़ाइल निकालें लिनक्स
- 13. भौतिक स्थान पर इसे बनाए बिना ज़िप फ़ाइल कैसे भेजें?
- 14. आयनिक ज़िप: बाइट से ज़िप फ़ाइल निर्माण []
- 15. ज़िप/लोन को ज़िप/पड़ोस नाम
- 16. क्यों ZipInputStream ZipOutputStream के आउटपुट को नहीं पढ़ सकता है?
- 17. ज़िप फ़ाइल
- 18. ज़िप फ़ाइल
- 19. ज़िप फ़ाइल
- 20. ज़िप कोड
- 21. ज़िप कोड
- 22. मैं रीयल टाइम में Response.Output पर फ्लाई और स्ट्रीम पर कैसे ज़िप करूं?
- 23. फ़ोल्डर में सबफ़ोल्डर सूचीबद्ध करें - मैटलैब (केवल उपफोल्डर्स, फाइल नहीं)
- 24. सभी आउटलुक फ़ोल्डरों और उपफोल्डर्स की सूची प्राप्त करें
- 25. मैं जावा में किसी मौजूदा ज़िप फ़ाइल में प्रविष्टियां कैसे जोड़ सकता हूं?
- 26. ज़िप ऑब्जेक्ट की सूची पर लेन को निष्पादित करना ज़िप
- 27. ज़िप पर निकालने के बिना ज़िप पर .mp4 फ़ाइल बजाना
- 28. रूबी - एक ज़िप फ़ाइल
- 29. डीएमजी या ज़िप फ़ाइल?
- 30. एक ज़िप इन-मेमोरी
मुझे आशा है कि आप इस सवाल का नहीं छोड़ा है और एक जवाब चिह्नित करने के लिए वापस हो जाएगा का उपयोग कर या कम से कम इंगित करें कि कौन सा उत्तर उपयोगी था। आपके 200+ प्रश्नों में से आपके पास लगभग 120 होंगे जिनके लिए चिह्नित उत्तर की आवश्यकता है (यदि इसे संतोषजनक उत्तर दिया गया है), प्रत्येक चिह्नित उत्तर आपको अतिरिक्त 2 प्रतिनिधि अंक अर्जित करेगा और आपके प्रश्न के भविष्य के दर्शकों को यह जानने के लिए मदद करेगा कि सबसे अच्छा जवाब क्या था । 120 * 2rep = 240 प्रतिनिधि आप प्राप्त कर सकते हैं। – slugster