帮助需要:zip文件流 [英] Help required: zip files streaming

查看:205
本文介绍了帮助需要:zip文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在网络中发送的压缩文件的问题。所有其他的格式,我能够派除了.ZIP ..

我尝试了很多我不就没怎么做..在$ C $词都写在客户端上传文件,这是建议由微软这里是的

我是能够创建压缩文件,如果我尝试打开它说,该文件的corrupted..size也改变不少。

下面是code

 公共无效UploadFile(字符串localFile,字符串的uploadURL)
    {

        HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(的uploadURL);

        req.Method =PUT;
        req.AllowWriteStreamBuffering = TRUE;

        //检索请求流,敷在的StreamWriter
        流reqStream = req.GetRequestStream();
        StreamWriter的wrtr =新的StreamWriter(reqStream);

        //打开本地文件


        流流= File.open方法(localFile,FileMode.Open);

        //通过本地文件循环读取每一行
        //和写入请求数据流缓存

        byte []的BUFF =新的字节[1024];
        INT读取动作;
        而((读取动作= Stream.Read(BUFF,0,1024))大于0)
        {
            wrtr.Write(BUFF);
        }



        Stream.Close();
        wrtr.Close();

        尝试
        {
            req.GetResponse();
        }
        赶上(例外EE)
        {

        }

        reqStream.Close();
 

请帮我...

感谢

解决方案

主要的问题是,你正在使用的StreamWriter,这是一个TextWriter的,专为文本的数据,而不是二进制文件一样zip文件。

此外有问题雅各布提到的,还以为你不关闭请求流您收到的答复后,直到一个事实 - 尽管这不会让任何区别,因为的StreamWriter 将首先关闭它。

下面是固定的code,改为使用使用语句(避免留下流开),到文件一个简单的通话类和更有意义的名称(IMO)。

 使用(流输出= req.GetRequestStream())
使用(流输入= File.OpenRead(localFile))
{
    //通过本地文件循环读取每一行
    //和写入请求数据流缓存

    byte []的缓冲区=新的字节[1024];
    INT读取动作;
    而((读取动作= input.Read(缓冲液,0,1024))大于0)
    {
        output.Write(缓冲液,0,读取动作);
    }
}
 

请注意,您可以方便地提取while循环到一个辅助方法:

 公共静态无效CopyStream(流输入,流输出)
{
    //通过本地文件循环读取每一行
    //和写入请求数据流缓存

    byte []的缓冲区=新的字节[1024];
    INT读取动作;
    而((读取动作= input.Read(缓冲液,0,1024))大于0)
    {
        output.Write(缓冲液,0,读取动作);
    }
}
 

从其他地方使用,只留下:

 使用(流输出= req.GetRequestStream())
使用(流输入= File.OpenRead(localFile))
{
    CopyStream(输入,输出);
}
 

i have problem in sending zip file in network.. all other formats i am able to send except .zip..

i tried a lot i dont no how to do this.. the code i have written at client side to upload file, this is suggested by microsoft here is the link

i am able to create zip file, if i try to open it says corrupted..size of the file also varying lot.

here is code

  public void UploadFile(string localFile, string uploadUrl)
    {

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);

        req.Method = "PUT";
        req.AllowWriteStreamBuffering = true;

        // Retrieve request stream and wrap in StreamWriter
        Stream reqStream = req.GetRequestStream();
        StreamWriter wrtr = new StreamWriter(reqStream);

        // Open the local file


        Stream Stream = File.Open(localFile, FileMode.Open);

        // loop through the local file reading each line 
        //  and writing to the request stream buffer 

        byte[] buff = new byte[1024];
        int bytesRead;
        while ((bytesRead = Stream.Read(buff, 0,1024)) > 0)
        {
            wrtr.Write(buff);
        }



        Stream.Close();
        wrtr.Close();

        try
        {
            req.GetResponse();
        }
        catch(Exception ee)
        {

        }

        reqStream.Close();

please help me...

Thanks

解决方案

The main problem is that you're using a StreamWriter, which is a TextWriter, designed for text data, not binary files like zip files.

Additionally there's the problem Jacob mentioned, and also the fact that you're not closing the request stream until after you've received the response - although that won't make any difference because the StreamWriter will close it first.

Here's the fixed code, changed to use using statements (to avoid leaving streams open), a simpler call to the File class, and more meaningful names (IMO).

using (Stream output = req.GetRequestStream())
using (Stream input = File.OpenRead(localFile))
{
    // loop through the local file reading each line 
    //  and writing to the request stream buffer 

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, 1024)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}

Note that you can easily extract the while loop into a helper method:

public static void CopyStream(Stream input, Stream output)
{
    // loop through the local file reading each line 
    //  and writing to the request stream buffer 

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, 1024)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}

to be used from other places, leaving just:

using (Stream output = req.GetRequestStream())
using (Stream input = File.OpenRead(localFile))
{
    CopyStream(input, output);
}

这篇关于帮助需要:zip文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆