我正在尝试创建一个文件传输应用程序,该应用程序在压缩文件后发送文件。我的主要目标是提供可恢复的传输,为此,我首先创建用户必须发送的所有文件的存档,然后我在流中打开存档并从中读取一大块数据(比如一个块2mb),我压缩该块并在接收端解压缩,然后将该数据附加到一个更大的文件中,该文件将成为主存档。
GZipOutputStream gzipout = new GZipOutputStream(File.Create("abc.zip"));
//reading and compressing the chunk into abc.zip
do
{
//f is my filestream for the original archive
sourceBytes = f.Read(buffer, 0, buffer.Length);
if (sourceBytes == 0)
{
break;
}
gzipout.Write(buffer, 0, sourceBytes);
bytesread += sourceBytes;
} while (bytesread < chunklength);
bytesread = 0;
gzipout.Finish();
gzipout.Close();
//encrypting the chunk
s.EncryptFile("abc.zip","abc.enc");
//the chunk is sent over here to the receiving side
//these are the functions on the receiving side
//decrypting the chunk
s.DecryptFile("abc.enc","abc1.zip");
GZipInputStream gzipin=new GZipInputStream(File.OpenRead("abc1.zip"));
//the chunk is being decompressed and appended into the original archive file
FileStream temp = File.Open(Path.GetFileName("originalzip.zip"), FileMode.Append);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = gzipin.Read(data, 0, data.Length);
if (size != 0)
temp.Write(data, 0, size);
else
break;
}
temp.Flush();
temp.Close();
} while (sourceBytes!=0);
ExtractAll("originalzip.zip", "testest");
问题出在分块过程的某个地方,因为如果我按原样发送存档,它可以工作,但是当我尝试制作分块时,接收存档的大小较大并且无法打开。
编辑:它是固定的,我在制作新块之前删除了先前创建的块并修复了它