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
| private void CompressFile(string inputPath, string outputPath, SPItemEventProperties properties)
{
byte[] compressedFile;
SPList list = properties.List;
SPWeb currentWeb = properties.Web;
FileInfo outFileInfo = new FileInfo(outputPath);
FileInfo inFileInfo = new FileInfo(inputPath);
// Compress
using (MemoryStream msOut = new MemoryStream())
{
using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(msOut))
{
zipStream.SetLevel(9);
ICSharpCode.SharpZipLib.Zip.ZipEntry newEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(inFileInfo.Name);
newEntry.DateTime = DateTime.UtcNow;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(inputPath))
{
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
zipStream.IsStreamOwner = true;
properties.List.RootFolder.Files.Add(currentWeb.Url + "/" + list.RootFolder.Name + "/" + NameWithExt(properties),zipStream,true);
zipStream.Close();
}
}
} |
Partager