回答編集履歴
3
誤記修正
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
削除リクエストは通らないらしいので、回答を準備しました。
|
2
2
|
ファイルを閉じずに、そのまま再利用する方法です。
|
3
|
-
実際の動作を見ていないので、
|
3
|
+
実際の動作を見ていないのと、質問されている内容に対する回答ではないので、ご了承ください。
|
4
4
|
|
5
5
|
|
6
6
|
問題を解決するかもしれない回答
|
2
aa
answer
CHANGED
@@ -1,12 +1,64 @@
|
|
1
|
-
削除リクエストは通らないらしいので、回答を準備
|
1
|
+
削除リクエストは通らないらしいので、回答を準備しました。
|
2
|
+
ファイルを閉じずに、そのまま再利用する方法です。
|
3
|
+
実際の動作を見ていないので、悪しからずご了承ください。
|
2
4
|
|
3
5
|
|
4
6
|
問題を解決するかもしれない回答
|
5
7
|
-----------
|
6
|
-
|
8
|
+
```
|
9
|
+
{
|
10
|
+
string path = @"C:\test\sample.csv";
|
7
11
|
|
12
|
+
//Output file path.
|
13
|
+
string OutFilePath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path)) + ".enc";
|
8
14
|
|
15
|
+
//CSV出力
|
16
|
+
using (var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
17
|
+
using (var streamWriter = new StreamWriter(fileStream, Encoding.GetEncoding("shift_jis")))
|
18
|
+
using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture))
|
19
|
+
{
|
20
|
+
csvWriter.Configuration.HasHeaderRecord = true;
|
21
|
+
csvWriter.Configuration.RegisterClassMap<EntityMappper>();
|
22
|
+
csvWriter.WriteRecords(EntityList);
|
23
|
+
// バッファ上のデータを書き出して、ポインタを先頭へ
|
24
|
+
fileStream.Flush();
|
25
|
+
fileStream.Seek(0, SeekOrigin.Begin);
|
9
26
|
|
27
|
+
// 暗号化
|
28
|
+
FileEncrypt(fileStream, OutFilePath);
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
private void FileEncrypt(FileStream input, string OutFilePath)
|
33
|
+
{
|
34
|
+
byte[] buffer = new byte[4096];
|
35
|
+
var salt = ASCIIEncoding.ASCII.GetBytes("salt1234");
|
36
|
+
using (AesManaged aes = new AesManaged())
|
37
|
+
{
|
38
|
+
|
39
|
+
//...
|
40
|
+
|
41
|
+
//Encryption interface.
|
42
|
+
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
|
43
|
+
// If file is existed file, throw
|
44
|
+
using (var outfs = new FileStream(OutFilePath, FileMode.Create, FileAccess.Write))
|
45
|
+
using (CryptoStream cse = new CryptoStream(outfs, encryptor, CryptoStreamMode.Write))
|
46
|
+
using (DeflateStream ds = new DeflateStream(cse, CompressionMode.Compress)) //圧縮
|
47
|
+
{
|
48
|
+
outfs.Write(salt, 0, 16); // salt をファイル先頭に埋め込む
|
49
|
+
outfs.Write(aes.IV, 0, 16); // 次にIVもファイルに埋め込む
|
50
|
+
|
51
|
+
int len;
|
52
|
+
while ( (len = input.Read(buffer, 0, 4096)) > 0)
|
53
|
+
{
|
54
|
+
ds.Write(buffer, 0, len);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
}
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
10
62
|
誤った回答
|
11
63
|
-----------
|
12
64
|
Pathを使用中(StreamWriterのusingブロック中)にFileStream(FileEncrypt)でさらに開こうとしています。
|
1
回答する気が全くなくなるな...
answer
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
削除リクエストは通らないらしいので、回答を準備できるまで今しばらくお待ちください。
|
2
|
+
|
3
|
+
|
4
|
+
問題を解決するかもしれない回答
|
5
|
+
-----------
|
6
|
+
作成中
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
誤った回答
|
11
|
+
-----------
|
1
12
|
Pathを使用中(StreamWriterのusingブロック中)にFileStream(FileEncrypt)でさらに開こうとしています。
|
2
13
|
usingブロックの外で呼び出す(下記例)か、インターフェイスを変更し、Streamを渡すように(未掲示)してみてください。
|
3
14
|
```
|