【问题标题】:SVN to ZIP on the flySVN 即时转 ZIP
【发布时间】:2009-05-07 23:22:47
【问题描述】:

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

现在,我想扩展 SVN 网页以提供“将 HEAD 下载为 ZIP”功能。像SourceForgeCodeplex 这样的门户网站确实提供了这个功能。

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

【问题讨论】:

  • 尝试在 serverfault.com 上发帖
  • @stukelly - 不同意; SVN 更像是一个“开发”的东西,而不是一个 it-pro 的东西。
  • @stukelly - 我无权访问 serverfault.com,此外,我是一名开发人员,我准备开发一个好的自定义解决方案。所以,我不仅仅追求现成的产品。

标签: c# svn zip


【解决方案1】:

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

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

我在下面的 C# 中的解决方案是使用 SharpSVNDotNetZip。完整的源代码可以在my SVN repository找到。

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

【讨论】:

    【解决方案2】:

    我不知道“内置”,但您可以尝试编写一个使用 SharpSVN 和#ZipLib 的页面来完成这项工作......

    如果这太慢了,您大概可以使用提交挂钩或计划作业(每隔几分钟或其他时间)将预先准备好的 zip 放在可以返回的地方 - 使用“create as a different名称然后在准备好时重命名”技巧以最大程度地减少锁定/不可用的时间。或者用修订号命名。

    【讨论】:

    • 这是一个想法。我有关于即时定制解决方案的想法,但担心它不会表现良好。 SourceForge 不只是使用 SVN 吗?还是他们有自己的软件来桥接 SVN?恐怕离线解决方案的配置太多了。我想从我想要的任何目录生成一个 zipfile。如果有一个很好的 SVN 替代方案可以做到这一点,我很乐意切换。
    【解决方案3】:

    TortoiseSVN 注册了一些浏览器可以识别的可插入协议。 svn://blahblahblah ...你可以调查一下。但是你必须使用 TortoiseSVN...

    【讨论】:

    • 我不知道这个,谢谢。但这不是我的问题的解决方案。我真的在寻找通过网络提供的服务器端解决方案。像 SourceForge/Codeplex 那样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2011-06-15
    • 1970-01-01
    • 2012-06-22
    • 2021-04-30
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多