private void AddToZip(ZipStorer zipStorer, string basePath, string path)
        {
            if (basePath == null)
            {
                return;
            }

            path = Path.GetFullPath(path);

            // If this is not inside basePath, lets change the basePath so at least some directories are kept
            if (!path.StartsWith(basePath))
            {
                basePath = Path.GetDirectoryName(path);
                if (basePath == null)
                {
                    return;
                }
            }

            if (Directory.Exists(path))
            {
                foreach (var file in Directory.GetFiles(path))
                {
                    AddToZip(zipStorer, basePath, file);
                }
                foreach (var dir in Directory.GetDirectories(path))
                {
                    AddToZip(zipStorer, basePath, dir);
                }
            }
            else if (File.Exists(path))
            {
                var nameInZip = path.Substring(basePath.Length);
                if (nameInZip.StartsWith("\\") || nameInZip.StartsWith("/"))
                {
                    nameInZip = nameInZip.Substring(1);
                }
                nameInZip = Path.Combine("files", nameInZip);

                zipStorer.AddFile(ZipStorer.Compression.Deflate, path, nameInZip, string.Empty);
            }
        }
示例#2
0
        private void AddToZip(ZipStorer zipStorer, string basePath, string path)
        {
            if (basePath == null)
                return;

            path = Path.GetFullPath(path);

            // If this is not inside basePath, lets change the basePath so at least some directories are kept
            if (!path.StartsWith(basePath))
            {
                basePath = Path.GetDirectoryName(path);
                if (basePath == null)
                    return;
            }

            if (Directory.Exists(path))
            {
                foreach (var file in Directory.GetFiles(path))
                    AddToZip(zipStorer, basePath, file);
                foreach (var dir in Directory.GetDirectories(path))
                    AddToZip(zipStorer, basePath, dir);
            }
            else if (File.Exists(path))
            {
                var nameInZip = path.Substring(basePath.Length);
                if (nameInZip.StartsWith("\\") || nameInZip.StartsWith("/"))
                    nameInZip = nameInZip.Substring(1);
                nameInZip = Path.Combine("files", nameInZip);

                zipStorer.AddFile(ZipStorer.Compression.Deflate, path, nameInZip, string.Empty);
            }
        }