コード例 #1
0
ファイル: CryptoZipLib.cs プロジェクト: Netdex/CSFileEncrypt
        public static void Zip(FileInfo file, byte[] key, string outputFolder, PercentChangedEventHandler percentChangedCallback)
        {
            if (string.IsNullOrEmpty(outputFolder))
                outputFolder = file.DirectoryName;

            FileStream inputStream = File.OpenRead(file.FullName);
            FileStream outputStream = File.OpenWrite(outputFolder + "\\" + file.Name + CRYPTOZIP_EXTENSION);

            Aes aes = new AesManaged();
            byte[] keyBytes = key;
            byte[] ivBytes = Encoding.UTF8.GetBytes(IV_STRING);

            using (CryptoStream cryptoStream = new CryptoStream(outputStream, aes.CreateEncryptor(keyBytes, ivBytes), CryptoStreamMode.Write))
            {
                using (ZipOutputStream zipOutputStream = new ZipOutputStream(cryptoStream))
                {
                    zipOutputStream.SetLevel(0);
                    zipOutputStream.UseZip64 = UseZip64.On;
                    ZipEntry zipEntry = new ZipEntry(file.Name);
                    zipEntry.DateTime = DateTime.Now;

                    zipOutputStream.PutNextEntry(zipEntry);
                    int read = 0;
                    byte[] buffer = new byte[4096];
                    double totalRead = 0;
                    double lastPercent = 0;

                    do
                    {
                        read = inputStream.Read(buffer, 0, buffer.Length);
                        zipOutputStream.Write(buffer, 0, read);

                        totalRead += read;
                        double percent = Math.Floor((totalRead / inputStream.Length) * 100);

                        if (percent > lastPercent)
                        {
                            lastPercent = percent;

                            if (percentChangedCallback != null)
                                percentChangedCallback(new PercentChangedEventArgs(percent));
                        }

                    } while (read == buffer.Length);

                    zipOutputStream.Finish();
                    zipOutputStream.Flush();
                }
            }
            inputStream.Close();
            outputStream.Close();
        }
コード例 #2
0
ファイル: CryptoZipLib.cs プロジェクト: Netdex/CSFileEncrypt
 public static void Zip(FileInfo file, byte[] key, PercentChangedEventHandler percentChangedCallback)
 {
     Zip(file, key, file.DirectoryName, percentChangedCallback);
 }