通过Internet共享Zip文件 [英] Sharing Zip File Over A Internet

查看:72
本文介绍了通过Internet共享Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有自己的专用服务器.通过与我的用户的100 Mbps下载连接,可以下载文件.
我有一个60KB的小文本文件,我正在服务器上不断将其转换为8KB大小的zip文件.因此,我的用户可以通过拨号连接轻松快速地下载此文件.
这里的问题是,每当我的客户尝试通过我的客户应用程序使用http协议下载此文件时,该zip文件都会在服务器上被阻止,直到客户完成下载为止.在这段阻塞时间内,我无法更新服务器上的zip文件.
现在,当200个客户端每秒同时下载此zip文件时,就会出现更多问题.我的压缩该文件的C#代码在服务器上被不确定地阻止.而且我无法以实时模式更新此zip文件.

我使用了try&catch语句的C#代码.但是,每次任何客户端下载此文件时,它仅属于捕获代码段.

我的问题是如何在实时模式下允许使用C#在服务器中压缩此文本文件,并且仍然允许1000个客户端在实时模式下访问此文件.

我在服务器上使用C#,ASP .NET Web服务.

如果您有解决此问题的方法,请告诉我.

Hi,

I have my own dedicated server. With 100 Mbps download connection to my users to download files.
I have a small 60KB text file that I am constantly converting into zip file with 8KB in size on my server. So my user can easily download this file with dial up connection very fast.
Problem here is that whenever my clients tried to download this file using http protocol through my client application, This zip file is blocked on server until client finishes his download. In between this blocked time I can''t update zip file on server.
Now problem arises more when 200 clients simultaneously download this zip file each second. My C# code of zipping this file blocked indefinably on server. And I can''t update this zip file in realtime mode.

I used c# code of try & catch statement. But it only fall into catch code segment each time any client downloading this file.

My question is how to allow zipping of this text file in server using C# in realtime mode & still allow 1000 clients to access this file in realtime mode.

I am using C#, ASP .NET web services on my server.

Please let me know if you have any solution to this problem.

推荐答案

1.读取文件并存储在内存流中.
2.压缩流,并允许用户仅下载压缩的流.
U可以使用开源SharpzipLib对其进行压缩:
1. Read your file and store in memory stream.
2. Zip the stream and allow users to to download the zipped stream only.
U can use Open Source SharpzipLib for zipping it:
/// <summary>
/// Get MemoryStream stream of the file path specified
/// </summary>
/// <param name="FileNamePath">File System full path and file name</param>
private MemoryStream GetMemoryStream(string filepath, out Exception e)
{
    e = null;
    MemoryStream dest = new MemoryStream();
    try
    {

        using (Stream source = File.OpenRead(filepath))
        {
            dest.SetLength(source.Length);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                dest.Write(buffer, 0, bytesRead);
            }
            dest.Flush();
            dest.Position = 0;
            return dest;
        }
    }
    catch (Exception ex)
    {
        e = new Exception("GetMemoryStream : " + ex.ToString());
        return (MemoryStream)null;
    }
}
        /// <summary>
        /// Get byte array fron MemoryStream stream
        /// </summary>
        /// <param name="stream">Stream to read</param>
        public byte[] ReadToEnd(Stream stream)
        {
            long originalPosition = 0;

            if (stream.CanSeek)
            {
                originalPosition = stream.Position;
                stream.Position = 0;
            }

            try
            {
                byte[] readBuffer = new byte[4096];

                int totalBytesRead = 0;
                int bytesRead;

                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;

                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                if (stream.CanSeek)
                {
                    stream.Position = originalPosition;
                }
            }
        }

        private byte[] ZipToByteArray(MemoryStream memStreamIn, string zipEntryName, string password)
        {

            MemoryStream outputMemStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
            zipStream.Password = password;
            zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

            ZipEntry newEntry = new ZipEntry(zipEntryName);
            newEntry.DateTime = DateTime.Now;


            zipStream.PutNextEntry(newEntry);

            StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
            zipStream.CloseEntry();

            zipStream.IsStreamOwner = false;	// False stops the Close also Closing the underlying stream.
            zipStream.Close();			        // Must finish the ZipOutputStream before using outputMemStream.

            outputMemStream.Flush();
            outputMemStream.Position = 0;
            return ReadToEnd(outputMemStream);
        }



最后是下载方法:



And finally the download method:

void Download(string fileName, byte[] data)
{
try
    {
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearContent();
        response.ClearHeaders();
        response.Buffer= true;
        response.AddHeader("Content-Disposition","attachment;filename=\"" + fileName  + "\"");
        response.BinaryWrite(data);
        response.End();
    }
    catch(Exception ex)
    {
    }
}


这篇关于通过Internet共享Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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