ドキュメントを抽出し、そのすべてのデータをtxtファイル形式で保存しています。抽出にはhttp://www.codeproject.com/Articles/31944/Implementing-a-TextReader-to-extract-various-filesこのライブラリを使用しています。ドキュメント抽出のために、私は以下のコードを使用しています:
public static void ExtractDocument()
{
List<string> lstFiles= new List<string>();
lstFiles.Add("123.txt");
lstFiles.Add("123.doc");
lstFiles.Add("123.docx");
foreach (var fileName in lstFiles)
{
string newFileName = Path.Combine("D://extractFiles//downloadPath", fileName);
string outputFilefiname = "D://extractFiles//vij.txt";
using (var target = new FilterReader(newFileName, 0x1000))
{
target.Init();
const int blockSize = 1024;
var buffer = new Char[blockSize];
var readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
while (readblockBufferSize > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readblockBufferSize);
}
readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
}
else
{
readblockBufferSize = readblockBufferSize - 1; --"may be due to this Line I have lost data"
}
}
}
// Write Delimete.
using (StreamWriter sWriter = new StreamWriter(outputFilefiname, true))
{
sWriter.WriteLine(string.Format("{0} 1111111111--2222222222-333333333-444444444-555555555 {0}", Environment.NewLine));
}
}
}
このメソッドでは、抽出は正常に機能しますが、抽出中に一部のデータが失われました
else
{
readblockBufferSize = readblockBufferSize - 1; --"may be due to this Line I have lost data"
}
しかし、以下の抽出用コードを使用している場合:
public static void ExtractDocument()
{
List<string> lstFiles = new List<string>();
lstFiles.Add("123.txt");
lstFiles.Add("123.doc");
lstFiles.Add("123.docx");
foreach (var fileName in lstFiles)
{
string newFileName = Path.Combine("D://extractFiles//downloadPath", fileName);
string outputFilefiname = "D://extractFiles//vij.txt";
using (var target = new FilterReader(newFileName, 0x1000))
{
target.Init();
const int blockSize = 1024;
var buffer = new Char[blockSize];
var readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
while (target.ReadBlock(buffer, 0, blockSize) > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readblockBufferSize);
}
}
}
}
// Write Delimete.
using (StreamWriter sWriter = new StreamWriter(outputFilefiname, true))
{
sWriter.WriteLine(string.Format("{0} 1111111111--2222222222-333333333-444444444-555555555 {0}", Environment.NewLine));
}
}
}
この場合、While ループは終了しません (無限ループ)。どこでやっているのか分析できない ミス。
更新された方法
public ResponseOutput ExtractFile(string outputFilefiname,string downloadedFileLocation,List<string> lstfiles)
{
var responseOutput = new ResponseOutput();
responseOutput.IsSuccessful = false;
int filescount = 0;
if (!Directory.Exists(downloadedFileLocation) || string.IsNullOrWhiteSpace(downloadedFileLocation))
{
return responseOutput;
}
try
{
foreach (var fileName in lstfiles)
{
responseOutput = new ResponseOutput();
responseOutput.IsSuccessful = false;
string finalExtractedFile = Path.Combine(downloadedFileLocation, fileName);
using (var target = new FilterReader(finalExtractedFile, 0x1000))
{
target.Init();
var buffer = new Char[1024];
int blockSize = buffer.Length;
int readBlockBufferSize = 0;
while ((readBlockBufferSize = target.ReadBlock(buffer, 0, blockSize)) > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readBlockBufferSize);
}
}
}
filescount = filescount + 1;
if (filescount != lstfiles.Count)
{
using (StreamWriter sWriter = new StreamWriter(outputFilefiname, true))
{
sWriter.WriteLine(string.Format("{0} {1} {0}", Environment.NewLine, FileSepratorDelimiter));
}
}
}
if(File.Exists(finalExtractedFile))
{
//File.Delete(finalExtractedFile);
}
}
}
catch (Exception ex)
{
//
}
return responseOutput;
}
どこで間違いを犯しているか、この問題を解決するために何をする必要があるかを教えてください。