コード例 #1
0
        private static void CompressionFile(string sourcePath, string folderToZippedFile, bool compress = true)
        {
            const int bufferSize = 16834;

            byte[] buffer   = new byte[bufferSize];
            string destPath = Path.Combine(folderToZippedFile, Path.GetFileName(sourcePath) + ".gz");
            MyFile sourceFile;

            if (Factory.TryGetFile(sourcePath, out sourceFile))
            {
                MyFile outFile = Factory.CreateFile(destPath);
                using (Stream inFileStream = sourceFile.FileOpen(FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (Stream outFileStream = outFile.FileOpen(FileMode.Create, FileAccess.Write, FileShare.Read))
                        using (GZipStream gZipStream = new GZipStream(compress ? outFileStream : inFileStream, compress ? CompressionMode.Compress : CompressionMode.Decompress))
                        {
                            Stream inStream  = compress ? inFileStream : gZipStream;
                            Stream outStream = compress ? gZipStream : outFileStream;
                            int    bytesRead = 0;
                            do
                            {
                                bytesRead = inStream.Read(buffer, 0, bufferSize);
                                outStream.Write(buffer, 0, bytesRead);
                            } while (bytesRead > 0);
                        }
            }
        }
コード例 #2
0
 public override void Visit(MyFile file)
 {
     if (crypt)
     {
         using (FileStream fin = file.FileOpen(FileMode.Open, FileAccess.Read, FileShare.None))
         {
             MyFile cryptedFile = Factory.CreateFile(file.FullPath + "_crypted");
             using (FileStream fout = cryptedFile.FileOpen(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
             {
                 fout.SetLength(0);
                 //Create variables to help with read and write.
                 byte[] bin    = new byte[100]; //This is intermediate storage for the encryption.
                 long   rdlen  = 0;             //This is the total number of bytes written.
                 long   totlen = fin.Length;    //This is the total length of the input file.
                 int    len;                    //This is the number of bytes to be written at a time.
                 DES    des = new DESCryptoServiceProvider();
                 using (CryptoStream encStream = new CryptoStream(fout, desProvider.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write))
                 {
                     //Read from the input file, then encrypt and write to the output file.
                     while (rdlen < totlen)
                     {
                         len = fin.Read(bin, 0, 100);
                         encStream.Write(bin, 0, len);
                         rdlen = rdlen + len;
                     }
                 }
             }
         }
         file.Delete();
     }
     else
     {
         bool success = true;
         using (FileStream fin = file.FileOpen(FileMode.Open, FileAccess.Read, FileShare.None))
         {
             MyFile decryptedFile;
             string pathToCryptedFile = file.FullPath;
             if (pathToCryptedFile.EndsWith("_crypted"))
             {
                 decryptedFile = Factory.CreateFile(file.FullPath.Remove(pathToCryptedFile.Length - "_crypted".Length));
             }
             else
             {
                 decryptedFile = Factory.CreateFile(pathToCryptedFile + "_decrypted");
             }
             using (FileStream fout = decryptedFile.FileOpen(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
             {
                 fout.SetLength(0);
                 //Create variables to help with read and write.
                 byte[] bin    = new byte[100]; //This is intermediate storage for the encryption.
                 long   rdlen  = 0;             //This is the total number of bytes written.
                 long   totlen = fin.Length;    //This is the total length of the input file.
                 int    len;                    //This is the number of bytes to be written at a time.
                 DES    des = new DESCryptoServiceProvider();
                 try
                 {
                     using (CryptoStream encStream = new CryptoStream(fout, desProvider.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write))
                     {
                         //Read from the input file, then decrypt and write to the output file.
                         while (rdlen < totlen)
                         {
                             len = fin.Read(bin, 0, 100);
                             encStream.Write(bin, 0, len);
                             rdlen = rdlen + len;
                         }
                     }
                 }
                 catch (CryptographicException exc)
                 {
                     System.Windows.Forms.MessageBox.Show(exc.Message + "Неверный ключ.");
                     success = false;
                 }
                 catch (IOException ioexc)
                 {
                     System.Windows.Forms.MessageBox.Show(ioexc.Message + "Ошибка доступа.");
                 }
                 catch (Exception exc)
                 {
                     System.Windows.Forms.MessageBox.Show(exc.Message + "Неопознанная ошибка.");
                 }
             }
             if (!success)
             {
                 decryptedFile.Delete();
             }
         }
         if (success)
         {
             file.Delete();
         }
     }
 }