how to unzip file and upload on ftp Ask Question
By   Junaid A     25-Apr-2024    2

How to upload file on ftp

how to unzip file and upload on ftp

Solution

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

 


public partial class AddFromFtp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "admin - " + Resources.Resource.webtitle;

        if (!IsPostBack)
        {
            if (Session["userid"] == null)
            {
              //  Response.Redirect("~/login.aspx");
            }

            LoadDDL();
        }
    
      
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        // This is necessary because Safari and Chrome browsers don't display the Menu control correctly.
        // All webpages displaying an ASP.NET menu control must inherit this class.
        if (Request.ServerVariables["http_user_agent"].IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)
            Page.ClientTarget = "uplevel";
    }

    private void LoadDDL()
    {
        ddlmaincate.DataSource = ChannelCategoryBLL.GetChannelCategoryList();
        ddlmaincate.DataTextField = "ChannelCategoryTitle";
        ddlmaincate.DataValueField = "ChannelCategoryID";
        ddlmaincate.DataBind();

        ddlmaincate.Items.Insert(0, new ListItem("-- select -", "0"));

       // LoadChannelByCatId();

    }
    private void LoadChannelByCatId()
    {
        ddlsubcate.DataSource = ChannelBLL.GetChannelListByChannelCategoryID(Convert.ToInt32(ddlmaincate.SelectedValue));
        ddlsubcate.DataTextField = "Channel";
        ddlsubcate.DataValueField = "ChannelID";
        ddlsubcate.DataBind();
    }
    protected void ddlmaincate_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadChannelByCatId();
    }
    protected void btnPushIntoDB_Click(object sender, EventArgs e)
    {
        try
        {
            UnzipFiles();
            PushToDB();
            lblresult.Text = "Files are pushed.";
        }
        catch (Exception ex)
        {

            lblresult.Text = ex.ToString();
        }
     
    }
    private void PushToDB()
    {
        // string[] filePaths = Directory.GetFiles((Server.MapPath("islamicdata", "*.xml", SearchOption.AllDirectories);
        //        string[] filePaths = Directory.GetFiles(Server.MapPath("islamicdata/beyaan"));
        string[] filePaths = Directory.GetFiles(Server.MapPath("islamicdata/" + ddldept4sub.SelectedItem.Text + "_" + ddlmaincate.SelectedItem.Text + "_" + ddlsubcate.SelectedItem.Text.Trim() + "/"));
        foreach (string var in filePaths)
        {
            string ss = var;

            // int pathindex = var.LastIndexOf("/");
            int pathindex = var.LastIndexOf("\\");
            string filename = var.Substring(pathindex + 1);
            // checking read files, filename start with "__", it mean file is already read.
            if (filename.StartsWith("KQI_") == false)
            {


                //// after reading Rename file


                FileInfo TheFile = new FileInfo(var);
                if (TheFile.Exists)
                {
                    int nfileind = var.LastIndexOf("\\");
                    string nfile = var.Insert(nfileind + 1, "KQI_");
                    try
                    {
                        SubChannel.SUBCHANNELSDataTable subchanneltbl = new SubChannel.SUBCHANNELSDataTable();
                        SubChannel.SUBCHANNELSRow row = (SubChannel.SUBCHANNELSRow)subchanneltbl.NewSUBCHANNELSRow();
                        row.ChannelID = Convert.ToDecimal(ddlsubcate.SelectedValue);
                        int lastindexoffile = filename.LastIndexOf(".");
                        row.SubChannelTitle = filename.Substring(0, lastindexoffile);
                        row.SubChannelDescription = row.SubChannelTitle;
                        row.ThumbnailFileName = filename;
                        row.MediaFileName = "KQI_" + filename;
                        if (filename.ToLower().Contains(".mp3") == true)
                        {
                            row.MediaFileType = true;
                        }
                        else
                        {
                            row.MediaFileType = false;
                        }
                        row.IsPodcastEnabled = true;
                        row.IsRSSEnabled = true;
                    
                        row.AddedDate = DateTime.Now;
                        //  row.AudioImageFileName = filename;
                        row.InterviewDate = DateTime.Now;
                        row.MediaDuration = "30:00";
                        row.ChannelKey = "channelkey";
                        row.NoOfDownloaded = 0;
                        row.NoOfViews = 0;
                        decimal tval1 = (TheFile.Length / 1024);
                        decimal tval = tval1 / 1024;
                        row.FileSize = Convert.ToString(System.Math.Round(tval, 2));

                        row.ChannelCategoryID = Convert.ToInt32(ddlmaincate.SelectedValue);
                      

                        decimal varsubchannelid = SubChannelBLL.AddSubChannel(ref row);
                        BusinessManager.SendClipToSubscribers(varsubchannelid.ToString(), row.SubChannelTitle);


                        File.Move(var, nfile);
                    }
                    catch (Exception ex)
                    {
                        lblresult.Text = ex.ToString();
                        File.Delete(nfile);
                        Console.WriteLine(ex.ToString());
                    }
                }


            }
        }
    }
    private void UnzipFiles()
    {
         
        //string newfilepath = Server.MapPath("islamicdata/" + ddldept4sub.SelectedItem.Text + "_" + ddlmaincate.SelectedItem.Text + "_" + ddlsubcate.SelectedItem.Text.Trim() + "/")";
        string filepathdetination =Server.MapPath("islamicdata/" + ddldept4sub.SelectedItem.Text + "_" + ddlmaincate.SelectedItem.Text + "_" + ddlsubcate.SelectedItem.Text.Trim() + "/");
        string[] filePaths = Directory.GetFiles(Server.MapPath("islamicdata/" + ddldept4sub.SelectedItem.Text + "_" + ddlmaincate.SelectedItem.Text + "_" + ddlsubcate.SelectedItem.Text.Trim() + "/"));
        foreach (string var in filePaths)
        {
            if (var.ToLower().Contains(".zip"))
            {
                string aa = var;


                {
                    int fi = var.LastIndexOf("\\");
                    string ffn = var.Substring(fi+1);
                    string ZipFileName = var.Substring(fi + 1);
                    string fpath =var;

                    try
                    {

                        if (!File.Exists(Server.MapPath(ZipFileName)))
                        {
                            //   lstProcess.Items.Add("File Does Not Exists.");
                            return;
                        }

                        using (ZipInputStream s = new ZipInputStream(File.OpenRead(Server.MapPath(ZipFileName))))
                        {

                            ZipEntry theEntry;
                            while ((theEntry = s.GetNextEntry()) != null)
                            {
                                string directoryName = Path.GetDirectoryName(theEntry.Name);
                                string fileName = Path.GetFileName(theEntry.Name);

                                // create directory
                                if (directoryName.Length > 0)
                                {
                                   // Directory.CreateDirectory(Server.MapPath(directoryName));
                                }

                                if (fileName != String.Empty)
                                {

                                    using (FileStream streamWriter = File.Create(filepathdetination + fileName))
                                    {

                                        int size = 2048;
                                        byte[] data = new byte[2048];
                                        while (true)
                                        {
                                            size = s.Read(data, 0, data.Length);
                                            if (size > 0)
                                            {
                                                streamWriter.Write(data, 0, size);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //Display Error
                    }
                    finally
                    {
                        //Update Status
                    }
                }
            }
        }
    }
    protected void ddldept4sub_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChannelCategoryDS.ChannelCategoryDataTable tblCat = ChannelCategoryBLL.GetCategoryByDepartamentID(Convert.ToInt32(ddldept4sub.SelectedValue));
        if (tblCat.Count >= 1)
        {
            ddlmaincate.DataSource = tblCat;
            ddlmaincate.DataTextField = "ChannelCategoryTitle";
            ddlmaincate.DataValueField = "ChannelCategoryID";
            ddlmaincate.DataBind();
            ddlmaincate.Items.Insert(0, new ListItem("-- select --", "0"));
            
        }
    }
    protected void btnunzp_Click(object sender, EventArgs e)
    {
        UnzipFiles();

    }
}

Solutions


10137
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM
03
07

New Messages

George Floyd
  • Edit Post Edit This Post within a Hour
  • Hide Chat Hide This Post
  • Delete Chat If inappropriate Post By Mistake
  • Report Inappropriate Chat
  • 4.5kb
  • Hi James! Please remember to buy the food for tomorrow! I’m gonna be handling the gifts and Jake’s gonna get the drinks
  • Hi James! Please remember to buy the food for tomorrow! I’m gonna be handling the gifts and Jake’s gonna get the drinks
  • Hi James! Please remember to buy the food for tomorrow! I’m gonna be handling the gifts and Jake’s gonna get the drinks