示例#1
0
 protected virtual void OnZip(ZipEventArgs e)
 {
     if (Zip != null)
     {
         Zip(this, e);
     }
 }
示例#2
0
        /// <summary>
        /// Add a folder along with its subfolders to a Package
        /// </summary>
        /// <param name="folderName">The folder to add</param>
        /// <param name="compressedFileName">The package to create</param>
        /// <param name="overrideExisting">Override exsisitng files</param>
        /// <returns>ReturnResult</returns>
        public ReturnResult PackageFolder(string folderName, string compressedFileName, bool overrideExisting)
        {
            if (folderName.EndsWith(@"\"))
            {
                folderName = folderName.Remove(folderName.Length - 1);
            }
            ReturnResult result = new ReturnResult();

            if (!Directory.Exists(folderName))
            {
                result.Message = "Source folder doesnt exist in" + folderName;
                return(result);
            }

            if (!overrideExisting && File.Exists(compressedFileName))
            {
                result.Message = "Destination file " + compressedFileName + " cannot be overwritten";
                return(result);
            }
            try
            {
                using (Package package = Package.Open(compressedFileName, FileMode.Create))
                {
                    var          fileList = Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories);
                    ZipEventArgs zipArgs  = new ZipEventArgs()
                    {
                        TotalFiles = fileList.Count(), FileNumber = 0, BytesZipped = 0, TotalBytes = 0
                    };
                    foreach (string fileName in fileList)
                    {
                        zipArgs.FileNumber++;
                        if (Path.GetFileName(fileName).IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
                        {
                            continue;
                        }
                        //The path in the package is all of the subfolders after folderName
                        string pathInPackage;
                        pathInPackage = Path.GetDirectoryName(fileName).Replace(folderName, string.Empty) + "/" + Path.GetFileName(fileName);

                        Uri         partUriDocument     = PackUriHelper.CreatePartUri(new Uri(pathInPackage, UriKind.Relative));
                        PackagePart packagePartDocument = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml, CompressionOption.Maximum);
                        using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                        {
                            fileStream.CopyTo(packagePartDocument.GetStream());
                            zipArgs.BytesZipped += fileStream.Length;
                            OnZip(zipArgs);
                        }
                        package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error zipping folder " + folderName, e);
            }
            OnZipEnd(new ZipEndEventArgs());
            result.Success = true;
            result.Message = "OK";
            return(result);
        }
示例#3
0
        /// <summary>
        /// Compress a file into a ZIP archive as the container store
        /// </summary>
        /// <param name="fileName">The file to compress</param>
        /// <param name="compressedFileName">The archive file</param>
        /// <param name="overrideExisting">override existing file</param>
        /// <returns>ReturnResult</returns>
        public ReturnResult PackageFile(string fileName, string compressedFileName, bool overrideExisting)
        {
            ReturnResult result = new ReturnResult();

            if (!File.Exists(fileName))
            {
                result.Message = "Source file doesnt exist in" + fileName;
                return(result);
            }

            if (!overrideExisting && File.Exists(compressedFileName))
            {
                result.Message = "Destination file " + compressedFileName + " cannot be overwritten";
                return(result);
            }

            try
            {
                Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri(Path.GetFileName(fileName), UriKind.Relative));

                using (Package package = Package.Open(compressedFileName, FileMode.Create))
                {
                    PackagePart packagePartDocument = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml, CompressionOption.Maximum);
                    using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        ZipEventArgs zipArgs = new ZipEventArgs()
                        {
                            TotalFiles = 1, FileNumber = 1, TotalBytes = new FileInfo(fileName).Length, BytesZipped = 0
                        };
                        OnZip(zipArgs);
                        fileStream.CopyTo(packagePartDocument.GetStream());
                        zipArgs.BytesZipped += fileStream.Length;
                        OnZip(zipArgs);
                    }
                    package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error zipping file " + fileName, e);
            }
            OnZipEnd(new ZipEndEventArgs());
            result.Success = true;
            result.Message = "OK";
            return(result);
        }