逻辑是这样的:
有一个大文件,格式如下
0123456
T Abc 123
T ffg 456
T you 789
1 X abc
...(省略具体的内容)
...
1 Y efg
...
...
1 Z klp
...
...
要求分割拆分成不同的文件,纵列第一个数字0到第一个1之间为Title,每个1需要按照Title+内容来生成文件如:x.gzip,y.gzip,z.gzip...现在我用代码实现了分割,且生成了gzip文件,但是用解压软件解开之后,用记事本打开却看到全部是NULL,如下图所示请大家指点一下,谢谢! string line = null;
StringBuilder strHeaderBuilder = new StringBuilder(line);
StreamReader strReader = null; byte[] buffer = null;
FileStream destinationStream = null;
GZipStream compressedStream = null; try
{
strReader = new StreamReader(txtSource.Text);
//Read Header
line = strReader.ReadLine();
//the first line must be start with char '0'.
if (string.IsNullOrEmpty(line) || line[0] != '0')
{
throw new Exception(this.ResourceManager.GetString("MSSCOR_MST_000350"));
} while (strReader.EndOfStream == false)
{
if (string.IsNullOrEmpty(line) == false && line[0] == '1')
{
break;
}
//Header
strHeaderBuilder.AppendLine(line);
line = strReader.ReadLine();
} if (strHeaderBuilder.Length == 0 || strReader.EndOfStream)
{ throw new Exception(this.ResourceManager.GetString("MSSCOR_MST_000350"));
} //the body has read one line.
while (true)
{
if (string.IsNullOrEmpty(line) == false && line[0] == '1')
{
if (compressedStream != null)
{
compressedStream.Close();
compressedStream = null;
}
if (destinationStream != null)
{
destinationStream.Close();
destinationStream = null;
}
String[] beginLines = line.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (beginLines.Length < 2)
{
throw new Exception(this.ResourceManager.GetString("MSSCOR_MST_000350"));
} string tmpPath = Path.Combine(txtDestination.Text, beginLines[1].Trim() + '.' + fileName + ".gzip");
destinationStream = new FileStream(tmpPath, FileMode.OpenOrCreate, FileAccess.Write);
compressedStream = new GZipStream(destinationStream, CompressionMode.Compress); buffer = new byte[strHeaderBuilder.Length];
compressedStream.Write(buffer, 0, buffer.Length);
buffer = new byte[line.Length];
compressedStream.Write(buffer, 0, buffer.Length);
}
else
{
buffer = new byte[line.Length];
compressedStream.Write(buffer, 0, buffer.Length);
} if (strReader.EndOfStream)
{
if (compressedStream != null)
{
compressedStream.Close();
compressedStream = null;
}
if (destinationStream != null)
{
destinationStream.Close();
destinationStream = null;
}
break;
}
line = strReader.ReadLine();
} }
finally
{
if (strReader != null)
{
strReader.Close();
}
if (destinationStream != null)
{
destinationStream.Close();
} if (compressedStream != null)
{
compressedStream.Close();
}
}
有一个大文件,格式如下
0123456
T Abc 123
T ffg 456
T you 789
1 X abc
...(省略具体的内容)
...
1 Y efg
...
...
1 Z klp
...
...
要求分割拆分成不同的文件,纵列第一个数字0到第一个1之间为Title,每个1需要按照Title+内容来生成文件如:x.gzip,y.gzip,z.gzip...现在我用代码实现了分割,且生成了gzip文件,但是用解压软件解开之后,用记事本打开却看到全部是NULL,如下图所示请大家指点一下,谢谢! string line = null;
StringBuilder strHeaderBuilder = new StringBuilder(line);
StreamReader strReader = null; byte[] buffer = null;
FileStream destinationStream = null;
GZipStream compressedStream = null; try
{
strReader = new StreamReader(txtSource.Text);
//Read Header
line = strReader.ReadLine();
//the first line must be start with char '0'.
if (string.IsNullOrEmpty(line) || line[0] != '0')
{
throw new Exception(this.ResourceManager.GetString("MSSCOR_MST_000350"));
} while (strReader.EndOfStream == false)
{
if (string.IsNullOrEmpty(line) == false && line[0] == '1')
{
break;
}
//Header
strHeaderBuilder.AppendLine(line);
line = strReader.ReadLine();
} if (strHeaderBuilder.Length == 0 || strReader.EndOfStream)
{ throw new Exception(this.ResourceManager.GetString("MSSCOR_MST_000350"));
} //the body has read one line.
while (true)
{
if (string.IsNullOrEmpty(line) == false && line[0] == '1')
{
if (compressedStream != null)
{
compressedStream.Close();
compressedStream = null;
}
if (destinationStream != null)
{
destinationStream.Close();
destinationStream = null;
}
String[] beginLines = line.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (beginLines.Length < 2)
{
throw new Exception(this.ResourceManager.GetString("MSSCOR_MST_000350"));
} string tmpPath = Path.Combine(txtDestination.Text, beginLines[1].Trim() + '.' + fileName + ".gzip");
destinationStream = new FileStream(tmpPath, FileMode.OpenOrCreate, FileAccess.Write);
compressedStream = new GZipStream(destinationStream, CompressionMode.Compress); buffer = new byte[strHeaderBuilder.Length];
compressedStream.Write(buffer, 0, buffer.Length);
buffer = new byte[line.Length];
compressedStream.Write(buffer, 0, buffer.Length);
}
else
{
buffer = new byte[line.Length];
compressedStream.Write(buffer, 0, buffer.Length);
} if (strReader.EndOfStream)
{
if (compressedStream != null)
{
compressedStream.Close();
compressedStream = null;
}
if (destinationStream != null)
{
destinationStream.Close();
destinationStream = null;
}
break;
}
line = strReader.ReadLine();
} }
finally
{
if (strReader != null)
{
strReader.Close();
}
if (destinationStream != null)
{
destinationStream.Close();
} if (compressedStream != null)
{
compressedStream.Close();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货