发送到带压缩文件夹的ftp [英] send to ftp in folder with zipped

查看:114
本文介绍了发送到带压缩文件夹的ftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用folderbrowserdialogue来获取文件夹,我想将其压缩并发送ftp服务器.
我写了这段代码,但是没用.
例外图片是: http://imageshack.us/f/11/82224641.png/ [ ^ ]


i want to take folder with folderbrowserdialogue and i want to zip it and send ftp server.
i write this code but didnt work.
exception picture is : http://imageshack.us/f/11/82224641.png/[^]


private static void Ziple(string sourceDirName, string destDirName, string pass)
        {   MemoryStream tempStream = new MemoryStream();
            ZipOutputStream zos = new ZipOutputStream(tempStream);

            FileInfo FI = new FileInfo(sourceDirName);
            tempStream.Position = 0;

            Uri remoteUri = new Uri(destDirName);


            UriBuilder builder = new UriBuilder(remoteUri);
            builder.Path += builder.Path.EndsWith("/") ? FI.Name : "/" + FI.Name;


            /* set up the FTP connection */
            FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create(builder.Uri);
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.KeepAlive = false;
            ftpRequest.UseBinary = true;

            ftpRequest.Credentials = new NetworkCredential(frmAdiSifre.register.GetValue("ad").ToString(), frmAdiSifre.register.GetValue("sifre").ToString());
            ftpRequest.ContentLength = tempStream.Length;

            try
            {
                const int buffLength = 2048;
                byte[] buff = new byte[buffLength];

                Stream strm = ftpRequest.GetRequestStream();

                int contentLen = tempStream.Read(buff, 0, buffLength);

                while (contentLen != 0)
                {
                    contentLen = tempStream.Read(buff, 0, buffLength);
                    strm.Write(buff, 0, contentLen);
                    
                }

                strm.Close();
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.ToString());
            }

            zos.Close();
 
          
        }

推荐答案

首先,我看不到您的zip方法有效.也会产生巨大的开销.
因此,让我们这样处理:

1.获取文件的内存流(代码效率也是reqd,读取块中的字节)
2.压缩内存流.
3.从压缩流中获取字节数组,并使用FTP请求将其写入.

First of all I do not see your zip method works. Also it makes huge overhead.
So lets approach this as:

1. Get memorystream for the file(code efficiency is also reqd, reading byte in chunks)
2. Zip the memorystream.
3. Get byte array from zipped stream and write it using FTP request.

////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;
    }
}





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);
}


////<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;
        }
    }
}


这篇关于发送到带压缩文件夹的ftp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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