/**/ /// <summary> /// Zip single file /// </summary> /// <param name="fileName">file name like "C:TestTest.txt"</param> private void ZipSingleFile(string fileNameToZip) { // Open and read this file FileStream fso = File.OpenRead(fileNameToZip); // Read this file to Buffer byte[] buffer = new byte[fso.Length]; fso.Read(buffer, 0, buffer.Length); // Create a new ZipEntry ZipEntry zipEntry = new ZipEntry(fileNameToZip.Split('\\')[fileNameToZip.Split('\\').Length - 1]); zipEntry.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. zipEntry.Size = fso.Length; fso.Close(); fso = null; // Using CRC to format the buffer this.m_Crc.Reset(); this.m_Crc.Update(buffer); zipEntry.Crc = this.m_Crc.Value; // Add this ZipEntry to the ZipStream this.m_ZipStream.PutNextEntry(zipEntry); this.m_ZipStream.Write(buffer, 0, buffer.Length); } #endregion #region IDisposable member /**/ /// <summary> /// Release all objects /// </summary> public void Dispose() { if (this.m_ZipStream != null) { this.m_ZipStream.Close(); this.m_ZipStream = null; } }
/// <summary> /// Create a zip archive sending output to the <paramref name="outputStream"/> passed. /// </summary> /// <param name="outputStream">The stream to write archive data to.</param> /// <param name="sourceDirectory">The directory to source files from.</param> /// <param name="recurse">True to recurse directories, false for no recursion.</param> /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param> /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param> /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks> public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter) { NameTransform = new ZipNameTransform(sourceDirectory); sourceDirectory_ = sourceDirectory; using ( outputStream_ = new ZipOutputStream(outputStream) ) { #if !NETCF_1_0 if ( password_ != null ) { outputStream_.Password = password_; } #endif outputStream_.UseZip64 = UseZip64; FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter); scanner.ProcessFile += new ProcessFileHandler(ProcessFile); if ( this.CreateEmptyDirectories ) { scanner.ProcessDirectory += new ProcessDirectoryHandler(ProcessDirectory); } if (events_ != null) { if ( events_.FileFailure != null ) { scanner.FileFailure += events_.FileFailure; } if ( events_.DirectoryFailure != null ) { scanner.DirectoryFailure += events_.DirectoryFailure; } } scanner.Scan(sourceDirectory, recurse); } }
/**/ /// <summary> /// Create Zip File by FileNameZipped /// </summary> /// <param name="fileNameZipped">zipped file name like "C:TestMyZipFile.ZIP"</param> private void CreateZipFile(string fileNameZipped) { this.m_Crc = new Crc32(); this.m_ZipStream = new ZipOutputStream(File.Create(fileNameZipped)); this.m_ZipStream.SetLevel(6); // 0 - store only to 9 - means best compression } #endregion #region CloseZipFile /**/ /// <summary> /// Close the Zip file /// </summary> private void CloseZipFile() { this.m_ZipStream.Finish(); this.m_ZipStream.Close(); this.m_ZipStream = null; }