Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# FTP ZIP
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 2-Sep-12 23:57pm
Comments
Bernhard Hiller at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
        }
    }
}
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 3 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100