[英]How create FileStream but not save?
I have code function 我有代码功能
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!");
}
}
I need return FileStream (fout) and fout not save to hard disk 我需要返回FileStream(fout)和fout不保存到硬盘
UPDATE: 更新:
YES, MemoryStream is good. 是的,MemoryStream很好。 but then I will need to use the FileStream otherwise an error occurs:
但是我需要使用FileStream,否则会发生错误:
not work: 不行:
using (ZipInputStream s = new ZipInputStream(fout))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)//exception
is work: 工作:
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
I need to decrypt the file, unzip it and then still get the text without saving 我需要解密文件,解压缩然后仍然得到文本而不保存
Don't use a second FileStream
. 不要使用第二个
FileStream
。 You can use a MemoryStream
instead. 您可以使用
MemoryStream
。
using (FileStream fin = File.OpenRead(inFile))
using(Stream fout = new MemoryStream())
...
I recommend changing your method signature to: 我建议您将方法签名更改为:
public static void DecryptFile(string inFile, string password, Stream outStream)
or 要么
public static void DecryptFile(string inFile, string password, string outFile)
(The 2nd can be implemented very easily by just calling the first with a FileStream
parameter). (第二个可以通过使用
FileStream
参数调用第一个来非常容易地实现)。
This leaves the responsibility of creating the Stream
with the caller, which has the advantage over Oded's solution that it does not necessarily store the entire output in memory; 这使得负责使用调用者创建
Stream
,这比Oded的解决方案更有优势,它不一定将整个输出存储在内存中; the user can choose to provide a Stream
that consumes the output as it is provided. 用户可以选择提供消耗输出的
Stream
。 This may be important if the file being decrypted is especially large. 如果被解密的文件特别大,这可能很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.