通过TCP(Golang)压缩和传输文件
我编写了简单的示例代码,它有效,但是收到的文件大小未压缩
我的客户端(用于连接到服务器并发送文件):
// connect to server
conn, err := net.Dial("tcp", serverAddr)
CheckError(err)
defer conn.Close()
in, err := os.Open(srcFile)
if err != nil {
log.Fatal(err)
}
pr, pw := io.Pipe()
gw, err := gzip.NewWriterLevel(pw, 7)
CheckError(err)
go func() {
n, err := io.Copy(gw, in)
gw.Close()
pw.Close()
log.Printf("copied %v %v", n, err)
}()
//maybe error some next?
_, err = io.Copy(conn, pr)
请帮助,如何正确使用带有副本的管道
I write simple example code, it worked, but size of file that recived is not compressed
My client (for connect to server and send file):
// connect to server
conn, err := net.Dial("tcp", serverAddr)
CheckError(err)
defer conn.Close()
in, err := os.Open(srcFile)
if err != nil {
log.Fatal(err)
}
pr, pw := io.Pipe()
gw, err := gzip.NewWriterLevel(pw, 7)
CheckError(err)
go func() {
n, err := io.Copy(gw, in)
gw.Close()
pw.Close()
log.Printf("copied %v %v", n, err)
}()
//maybe error some next?
_, err = io.Copy(conn, pr)
Please, help, how right to use pipe with copy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中已经说过的,你的代码可以工作。我创建了一个小例子来测试或看看是否可以解决您的问题。所以我想你可以结束这个问题了。
该程序的输出是一个简单的文本文件,其中包含许多重复的字符,需要压缩一些内容:该文件有 153 个字节,我发送/接收了 46 个字节
As I already said in the comment, your code works. I created a little example to test or see if I can solve your problem. So I guess you can close this question.
The output of that program with a simple text file with many repeated characters in it, to have something to compress: The file is 153 bytes and I send/received 46 bytes