ZIP compress all files (*.txt) in a directory and subdirectory c# -


i need help, new c#.

i have program compress folder files , folders, compress specific kind of file.

i use code compress:

if (!file.exists(name_of_zip_folder)) {     zipfile.createfromdirectory(folder, name_of_zip_folder); } 

i following:

public void zipfunction(string folder, list<string> files_to_compress){     //compress these kind of files, keeping structur of main folder } 

for example:

zipfunction(main_folder, new list<string> { "*.xlsx", "*.html"}); 

how compress specific file types?

this code snippet jan welker (originally posted here) shows how compress individual files using sharpziplib

private static void writezipfile(list<string> filestozip, string path, int compression) {  if (compression < 0 || compression > 9)     throw new argumentexception("invalid compression rate.");  if (!directory.exists(new fileinfo(path).directory.tostring()))     throw new argumentexception("the path not exist.");  foreach (string c in filestozip)     if (!file.exists(c))         throw new argumentexception(string.format("the file{0}does not exist!", c));   crc32 crc32 = new crc32(); zipoutputstream stream = new zipoutputstream(file.create(path)); stream.setlevel(compression);  (int = 0; < filestozip.count; i++) {     zipentry entry = new zipentry(path.getfilename(filestozip[i]));     entry.datetime = datetime.now;      using (filestream fs = file.openread(filestozip[i]))     {         byte[] buffer = new byte[fs.length];         fs.read(buffer, 0, buffer.length);         entry.size = fs.length;         fs.close();         crc32.reset();         crc32.update(buffer);         entry.crc = crc32.value;         stream.putnextentry(entry);         stream.write(buffer, 0, buffer.length);     } } stream.finish(); stream.close(); 

}

now combinding gets files specific folder , should have asking if did not missunderstand request?

var d = new directoryinfo(@"c:\temp"); fileinfo[] files = d.getfiles("*.txt"); //get txt files list<string> filestozip = new list<string>(); foreach(fileinfo file in files ) {     filestozip.add(file.name); } 

hope helps //kh.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -