-2
我正在做一個關於解壓縮tar
或tar.gz
文件的項目,我訪問了很多網站,而且我已經完成了這些功能,但現在我需要調查加快這個項目的方法。 大多數使用TarInputStream
來打開和讀取壓縮文件,並使用streamWriter
寫入壓縮文件中的數據到新的創建文件(空文件),我使用streamWriter
與線程結合。有沒有另外一種方法來寫入文件中的數據?
我的選擇是使用每個線程來捕獲壓縮文件中的數據,一個線程一個文件(如果壓縮包含大量文件),並且所有線程一起運行,或者給它一個線程並忽略它,但當我跑我的項目.exe文件,會出現此消息
該進程無法訪問該文件,因爲它正被另一個進程使用
所以,我想,也許StreamWriter的有螺紋的一些問題安全。
這裏是我的代碼
//write the data from compress file into new file
public static void StreamWriterFun(string pathToTar , string[] args, List<byte[]> mydata)
{
string[] str = new string[3];
for (int i = 0; i < args.Length; i++)
{
str[i] = args[i];
}
string directoryName = "";
long size = 0;
if (pathToTar != "")
directoryName = Path.GetDirectoryName(pathToTar) + "\\";
string fileName = Path.GetFileName(pathToTar);
Directory.CreateDirectory(str[1] + "\\" + directoryName);
if (fileName != "")
{
if ((File.Exists(str[1] + "\\" + directoryName + fileName)) || (!File.Exists(str[1] + "\\" + directoryName + fileName)))
{
string test = "";
using (FileStream streamWriter = File.Create(str[1] + "\\" + directoryName + fileName))
{
for (int i = 0; i < mydata.Count; i++)
{
streamWriter.Write(mydata[i], 0, mydata[i].Length);
}
streamWriter.Close();
}
}
}
}
public static void TGZWriteCharacters(string[] args)
{
//args[] define
string[] str = new string[3];
for (int i = 0; i < args.Length; i++)
{
str[i] = args[i];
}
try
{
Program file = new Program();
file.ExtractGZipSample(str[0], str[1]);
string[] filePaths = Directory.GetFiles(@str[1], "*.tar", SearchOption.TopDirectoryOnly);
str[2] = filePaths[0];
if (str[1] == "")
str[1] = Directory.GetCurrentDirectory();
if (!str[1].EndsWith("\\"))
str[1] = str[1] + "\\";// C:\Tar.gz
ArrayList myList = new ArrayList();
using (TarInputStream s = new TarInputStream(File.OpenRead(str[2])))// C:\python.tar
{
TarEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
myList.Add(theEntry.Name);
}
s.Close();
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
using (TarInputStream s = new TarInputStream(File.OpenRead(str[2])))
{
TarEntry theEntry;
String[] myArray = (String[])myList.ToArray(typeof(string));
for (int i = 0; i < myArray.Length-1; i++)
{
Thread[] test = new Thread[myArray.Length];
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToTar = "";
pathToTar = theEntry.Name;
if (pathToTar != "")
directoryName = Path.GetDirectoryName(pathToTar) + "\\";
string fileName = Path.GetFileName(pathToTar);
Directory.CreateDirectory(str[1] + directoryName);
if (fileName != "")
{
if ((File.Exists(str[1] + directoryName + fileName)) || (!File.Exists(str[1] + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(str[1] + directoryName + fileName))
{
int size = 2048;
int count = 0;
List<byte[]> mydatalist = new List<byte[]>();
byte[] mydataArray = new byte[2048];
while (true)
{
byte[] data = new byte[2048];
size = s.Read(data, 0, data.Length);
if (size > 0)
{
mydatalist.Add(data);
}
else
{
break;
}
}
test[i] = new Thread(() =>
StreamWriterFun(pathToTar, args, mydatalist)
);
test[i].Start();
streamWriter.Close();
}
}
}
}
}
s.Close();
System.IO.File.Delete(str[2]); // delete the tar file
}
sw.Stop();
string result1 = sw.Elapsed.TotalMilliseconds.ToString();
Console.WriteLine(result1);
}
catch (Exception)
{
Console.WriteLine("error1");
}
}
有另一種方式從compreesion數據寫入新文件?不使用streamWriter。
你發現了什麼問題?你能分享代碼嗎?可能是我們可以幫助你解決問題 –
這可能比不使用線程慢。 – Enigmativity
是的,使用線程是不可避免的,但線程中沒有辦法不使用streamwriter? –