Possible Duplicates:
how to read a *.file how to compress and uncompress a text file
Hi,
I am having a problem in uncompressing the zip file into a text file,
when i compress the text file into a zip file (Ex-20110530.txt to 20110530.zip) .my code is compressing the code i use for compressing the text file to a zip file is
string path = (string)(Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_atoz" + "\\" + name_atoz);
StreamWriter Strwriter = new StreamWriter(path);
DirectoryInfo di = new DirectoryInfo(path);
FileInfo fi = new FileInfo(path);
Compress(fi);
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Name != ".zip")
{
// Create the compressed file.
using (FileStream outFile = File.Create(System.Text.RegularExpressions.Regex.Replace(fi.FullName, ".txt$", "") + ".zip"))
{
using (GZipStream Compress = new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into the compression stream.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
Console.WriteLine("Compressed 开发者_运维知识库{0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}
this code creates a zip file when i unzip the file it creates a file called 20110530 not a 20110530.txt .
I want the file to be uncompress with 20110530.txt format
the code i use to uncompress the file is
public static void Decompress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
//Create the decompressed file.
using (FileStream outFile = File.Create(origName))
{
using (GZipStream Decompress = new GZipStream(inFile,
CompressionMode.Decompress))
{
//Copy the decompression stream into the output file.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0)
{
outFile.Write(buffer, 0, numRead);
}
Console.WriteLine("Decompressed: {0}", fi.Name);
}
}
}
}
Can Anyone Pls Help Me,
Thanks In Advance.
Seems to me that this line:
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
Removes the extension from the string, but you use that line when creating the uncompressed stream.
using (FileStream outFile = File.Create(origName))
Instead, use this:
using (FileStream outFile = File.Create(origName + ".txt"))
and that should create the uncompressed file with a TXT
extension.
That is because you remove the extension when you compress the file so that it becomes 20110530.zip
instead of 20110530.txt.zip
. The code that decompresses the file uses everything before .zip
as file name, so naturally you end up with 20110530
instead of 20110530.txt
.
Make sure to include the original file extension when you create a file in Decompress
//Create the decompressed file.
using (FileStream outFile = File.Create(origName)) // <<<-- here
精彩评论