将文件夹上传到目录服务器

发布于 2024-11-30 03:23:15 字数 172 浏览 0 评论 0原文

我想将保持相同结构(文件夹、子文件夹..)的整个文件夹上传到远程服务器。有必要迭代所有文件夹还是可以获取文件夹并上传到服务器?

我可以上传单个文件,但我认为文件夹的策略可能(当然)是不同的。

有什么建议吗?

谢谢

编辑:是远程服务器

I want to upload an entire folder keeping the same structure (folder, subfolders..) to a remote server. It´s necessary to iterate all folder or is possible to get the folder and upload to the server ?

I can upload single files but I think that the strategy with the folders maybe (sure) is different.

Any suggestion?

Thanks

EDIT: Is a remote server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-12-07 03:23:15

我不确定这就是您正在寻找的内容,但有时如果您可以在客户端进行管理,上传压缩文件夹(压缩级别较低)并将其解压到服务器上可能会更容易。如果它适用于您,您可以使用免费的 .net zip 库,例如 SharpZipLib,这样您就不需要自己编写压缩例程。

这也是使用 SharZipLib 压缩/解压缩文件夹的类:

using System;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace ENSI.Releaser.Code
{
    public class ZipUtility
    {
        public void ZipFiles(string inputFolderPath, string outputPathAndFile, string     password)
    {
        ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
        int trimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
        // find number of chars to remove     // from orginal file path
        trimLength += 1; //remove '\'
        FileStream ostream;
        byte[] obuffer;
        string outPath = inputFolderPath + @"\" + outputPathAndFile;
        var oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
        if (!string.IsNullOrEmpty(password))
            oZipStream.Password = password;
        oZipStream.SetLevel(9); // maximum compression
        ZipEntry oZipEntry;
        foreach (string fil in ar) // for each file, generate a zipentry
        {
            oZipEntry = new ZipEntry(fil.Remove(0, trimLength));
            oZipStream.PutNextEntry(oZipEntry);

            if (!fil.EndsWith(@"/")) // if a file ends with '/' its a directory
            {
                ostream = File.OpenRead(fil);
                obuffer = new byte[ostream.Length];
                ostream.Read(obuffer, 0, obuffer.Length);
                oZipStream.Write(obuffer, 0, obuffer.Length);
            }
        }
        oZipStream.Finish();
        oZipStream.Close();
    }

    private ArrayList GenerateFileList(string dir)
    {
        var fils = new ArrayList();
        bool Empty = true;
        foreach (string file in Directory.GetFiles(dir)) // add each file in directory
        {
            fils.Add(file);
            Empty = false;
        }

        if (Empty)
        {
            if (Directory.GetDirectories(dir).Length == 0)
            // if directory is completely empty, add it
            {
                fils.Add(dir + @"/");
            }
        }

        foreach (string dirs in Directory.GetDirectories(dir)) // recursive
        {
            foreach (object obj in GenerateFileList(dirs))
            {
                fils.Add(obj);
            }
        }
        return fils; // return file list
    }

    public void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
    {
        var s = new ZipInputStream(File.OpenRead(zipPathAndFile));
        if (!string.IsNullOrEmpty(password))
            s.Password = password;
        ZipEntry theEntry;
        string tmpEntry = String.Empty;
        while ((theEntry = s.GetNextEntry()) != null)
        {
            string directoryName = outputFolder;
            string fileName = Path.GetFileName(theEntry.Name);
            // create directory 
            if (directoryName != "")
            {
                Directory.CreateDirectory(directoryName);
            }
            if (fileName != String.Empty)
            {
                if (theEntry.Name.IndexOf(".ini") < 0)
                {
                    string fullPath = directoryName + "\\" + theEntry.Name;
                    fullPath = fullPath.Replace("\\ ", "\\");
                    string fullDirPath = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                    FileStream streamWriter = File.Create(fullPath);
                    int size = 2048;
                    byte[] data = new byte[size];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
        }
        s.Close();
        if (deleteZipFile)
            File.Delete(zipPathAndFile);
    }
}
}

I'm not sure this is what you are looking for, but sometimes it might be easier to upload the zipped folder (with low compression level) and unpack it on the server, if you can manage this on a client side. If it is applicable for you you might use free .net zip library like the SharpZipLib, so you don't have to write the zipping routine by yourself.

Here is also the class for zipping/unzipping folders using thr SharZipLib:

using System;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace ENSI.Releaser.Code
{
    public class ZipUtility
    {
        public void ZipFiles(string inputFolderPath, string outputPathAndFile, string     password)
    {
        ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
        int trimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
        // find number of chars to remove     // from orginal file path
        trimLength += 1; //remove '\'
        FileStream ostream;
        byte[] obuffer;
        string outPath = inputFolderPath + @"\" + outputPathAndFile;
        var oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
        if (!string.IsNullOrEmpty(password))
            oZipStream.Password = password;
        oZipStream.SetLevel(9); // maximum compression
        ZipEntry oZipEntry;
        foreach (string fil in ar) // for each file, generate a zipentry
        {
            oZipEntry = new ZipEntry(fil.Remove(0, trimLength));
            oZipStream.PutNextEntry(oZipEntry);

            if (!fil.EndsWith(@"/")) // if a file ends with '/' its a directory
            {
                ostream = File.OpenRead(fil);
                obuffer = new byte[ostream.Length];
                ostream.Read(obuffer, 0, obuffer.Length);
                oZipStream.Write(obuffer, 0, obuffer.Length);
            }
        }
        oZipStream.Finish();
        oZipStream.Close();
    }

    private ArrayList GenerateFileList(string dir)
    {
        var fils = new ArrayList();
        bool Empty = true;
        foreach (string file in Directory.GetFiles(dir)) // add each file in directory
        {
            fils.Add(file);
            Empty = false;
        }

        if (Empty)
        {
            if (Directory.GetDirectories(dir).Length == 0)
            // if directory is completely empty, add it
            {
                fils.Add(dir + @"/");
            }
        }

        foreach (string dirs in Directory.GetDirectories(dir)) // recursive
        {
            foreach (object obj in GenerateFileList(dirs))
            {
                fils.Add(obj);
            }
        }
        return fils; // return file list
    }

    public void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
    {
        var s = new ZipInputStream(File.OpenRead(zipPathAndFile));
        if (!string.IsNullOrEmpty(password))
            s.Password = password;
        ZipEntry theEntry;
        string tmpEntry = String.Empty;
        while ((theEntry = s.GetNextEntry()) != null)
        {
            string directoryName = outputFolder;
            string fileName = Path.GetFileName(theEntry.Name);
            // create directory 
            if (directoryName != "")
            {
                Directory.CreateDirectory(directoryName);
            }
            if (fileName != String.Empty)
            {
                if (theEntry.Name.IndexOf(".ini") < 0)
                {
                    string fullPath = directoryName + "\\" + theEntry.Name;
                    fullPath = fullPath.Replace("\\ ", "\\");
                    string fullDirPath = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                    FileStream streamWriter = File.Create(fullPath);
                    int size = 2048;
                    byte[] data = new byte[size];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
        }
        s.Close();
        if (deleteZipFile)
            File.Delete(zipPathAndFile);
    }
}
}
挽你眉间 2024-12-07 03:23:15

如果我正确理解您的问题,您可以:

A)在服务器上重新创建文件夹结构并将文件移动到那里

B)压缩根文件夹,将其移动到服务器并解压缩。

If I rigth understood your question you can:

A) recreate folder structure on the server and move the files there

B) zip your root folder, move it to server and unzip.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文