1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| private static ZipOutputStream addFile(ZipOutputStream zip, string pathFile, long length)
{
byte[] buffer = new byte[4096];
FileStream fs = new FileStream(pathFile, FileMode.Open);
ZipEntry entry = new ZipEntry(name);
entry.Size = length;
zip.PutNextEntry(entry);
int count = fs.Read(buffer, 0, buffer.Length);
while (count > 0)
{
zip.Write(buffer, 0, count);
count = fs.Read(buffer, 0, buffer.Length);
}
fs.Close();
return zip;
}
private static ZipOutputStream addDirecotry(ZipOutputStream zip, string pathDir)
{
DirectoryInfo currentdir = new DirectoryInfo(pathDir);
DirectoryInfo[] dirs = currentdir.GetDirectories();
FileInfo[] files = currentdir.GetFiles();
foreach (FileInfo f in files)
{
zip = FileSystem.addFile(zip, f.FullName, f.Length);
}
foreach (DirectoryInfo d in dirs)
{
zip = FileSystem.addDirecotry(zip, d.FullName);
}
return zip;
}
public static ZipOutputStream getZip(string path, Stream ouput)
{
ZipOutputStream zipOutputStream = new ZipOutputStream(ouput);
zipOutputStream.SetLevel(0); //0-9, 9 being the highest level of compression
ZipOutputStream test = new ZipOutputStream(ouput);
zipOutputStream = FileSystem.addDirecotry(zipOutputStream, path);
zipOutputStream.Close();
return zipOutputStream;
} |
Partager