SVN即时邮寄

tof*_*fi9 3 c# svn zip

我在我的Windows 2003服务器上安装了VisualSVN,并将其配置为提供匿名读访问.根据我的理解,VisualSVN只使用apache和下面的官方SVN Repository服务器.

现在,我想扩展SVN网页以提供"下载HEAD as ZIP"功能.像SourceForgeCodeplex这样的Web门户确实提供了这种功能.

是否有SVN Repository服务器的插件?或者可能是一个单独的Web客户端(最好是ASP.NET)?

tof*_*fi9 5

我找到了一个解决方案,并希望与您分享,以防其他人想要实现相同的解决方案.

在分析了WebSvn之后,我发现他们使用SVN导出目录功能将源代码下载到本地文件夹,然后即时压缩目录.表现相当不错.

我在C#中的解决方案是使用SharpSVNDotNetZip.完整的源代码可以在我的SVN存储库中找到.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSvn;
using Ionic.Zip;
using System.IO;
using SharpSvn.Security;

namespace SvnExportDirectory
{
    public class SvnToZip
    {
        public Uri SvnUri { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

        private bool passwordSupplied;

        public SvnToZip() { }

        public SvnToZip(Uri svnUri)
        {
            this.SvnUri = svnUri;
        }

        public SvnToZip(string svnUri)
            : this(new Uri(svnUri)) { }

        public void ToFile(string zipPath)
        {
            if (File.Exists(zipPath))
                File.Delete(zipPath);

            using (FileStream stream = File.OpenWrite(zipPath))
            {
                this.Run(stream);
            }
        }

        public MemoryStream ToStream()
        {
            MemoryStream ms = new MemoryStream();
            this.Run(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

        private void Run(Stream stream)
        {
            string tmpFolder =
                Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            try
            {
                using (SvnClient client = new SvnClient())
                {
                    //client.Authentication.Clear();
                    client.Authentication.UserNamePasswordHandlers += Authentication_UserNamePasswordHandlers;

                    SvnUpdateResult res;
                    bool downloaded = client.Export(SvnTarget.FromUri(SvnUri), tmpFolder, out res);
                    if (downloaded == false)
                        throw new Exception("Download Failed");
                }

                using (ZipFile zipFile = new ZipFile())
                {
                    zipFile.AddDirectory(tmpFolder, GetFolderName());
                    zipFile.Save(stream);
                }
            }
            finally
            {
                if (File.Exists(tmpFolder))
                    File.Delete(tmpFolder);
            }
        }


        private string GetFolderName()
        {
            foreach (var potential in SvnUri.Segments.Reverse())
            {
                if (string.Equals(potential, "trunk", StringComparison.InvariantCultureIgnoreCase) == false)
                    return potential;
            }
            return null;
        }

        void Authentication_UserNamePasswordHandlers(object sender, SvnUserNamePasswordEventArgs e)
        {
            if (passwordSupplied)
            {
                e.Break = true;
            }
            else
            {
                if (this.Username != null)
                    e.UserName = this.Username;

                if (this.Password != null)
                    e.Password = this.Password;

                passwordSupplied = true;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)