簡體   English   中英

如何創建FileStream但不保存?

[英]How create FileStream but not save?

我有代碼功能

public static void DecryptFile(string inFile, string outFile, string password)
{
    // create and open the file streams
    using (FileStream fin = File.OpenRead(inFile),
              fout = File.OpenWrite(outFile))
    {
        int size = (int)fin.Length; // the size of the file for progress notification
        byte[] bytes = new byte[BUFFER_SIZE]; // byte buffer
        int read = -1; // the amount of bytes read from the stream
        int value = 0;
        int outValue = 0; // the amount of bytes written out

        // read off the IV and Salt
        byte[] IV = new byte[16];
        fin.Read(IV, 0, 16);
        byte[] salt = new byte[16];
        fin.Read(salt, 0, 16);

        // create the crypting stream
        SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
        sma.IV = IV;

        value = 32; // the value for the progress
        long lSize = -1; // the size stored in the input stream

        // create the hashing object, so that we can verify the file
        HashAlgorithm hasher = SHA256.Create();

        // create the cryptostreams that will process the file
        using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read),
                  chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
        {
            // read size from file
            BinaryReader br = new BinaryReader(cin);
            lSize = br.ReadInt64();
            ulong tag = br.ReadUInt64();

            if (FC_TAG != tag)
                throw new CryptoHelpException("File Corrupted!");

            //determine number of reads to process on the file
            long numReads = lSize / BUFFER_SIZE;

            // determine what is left of the file, after numReads
            long slack = (long)lSize % BUFFER_SIZE;

            // read the buffer_sized chunks
            for (int i = 0; i < numReads; ++i)
            {
                read = cin.Read(bytes, 0, bytes.Length);
                fout.Write(bytes, 0, read);
                chash.Write(bytes, 0, read);
                value += read;
                outValue += read;
            }

            // now read the slack
            if (slack > 0)
            {
                read = cin.Read(bytes, 0, (int)slack);
                fout.Write(bytes, 0, read);
                chash.Write(bytes, 0, read);
                value += read;
                outValue += read;
            }
            // flush and close the hashing stream
            chash.Flush();
            chash.Close();

            // flush and close the output file
            fout.Flush();
            fout.Close();

            // read the current hash value
            byte[] curHash = hasher.Hash;

            // get and compare the current and old hash values
            byte[] oldHash = new byte[hasher.HashSize / 8];
            read = cin.Read(oldHash, 0, oldHash.Length);
            if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash)))
                throw new CryptoHelpException("File Corrupted!");
        }

        // make sure the written and stored size are equal
        if (outValue != lSize)
            throw new CryptoHelpException("File Sizes don't match!");
    }
}

我需要返回FileStream(fout)和fout不保存到硬盤

更新:

是的,MemoryStream很好。 但是我需要使用FileStream,否則會發生錯誤:

不行:

using (ZipInputStream s = new ZipInputStream(fout))
{

  ZipEntry theEntry;
  while ((theEntry = s.GetNextEntry()) != null)//exception

工作:

using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
{

  ZipEntry theEntry;
  while ((theEntry = s.GetNextEntry()) != null)

我需要解密文件,解壓縮然后仍然得到文本而不保存

不要使用第二個FileStream 您可以使用MemoryStream

using (FileStream fin = File.OpenRead(inFile))
  using(Stream fout = new MemoryStream())
...

我建議您將方法簽名更改為:

public static void DecryptFile(string inFile, string password, Stream outStream)

要么

public static void DecryptFile(string inFile, string password, string outFile)

(第二個可以通過使用FileStream參數調用第一個來非常容易地實現)。

這使得負責使用調用者創建Stream ,這比Oded的解決方案更有優勢,它不一定將整個輸出存儲在內存中; 用戶可以選擇提供消耗輸出的Stream 如果被解密的文件特別大,這可能很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM