Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
 
          
        }
Posted
Comments
Bernhard Hiller 3-Sep-12 6:14am    
Post the error message here.
And if a line number is shown, also highlight the line causing the error in your code snippet.

1 solution

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.

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


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

C#
////<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;
        }
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900