SetComment() public method

Set the file comment to be recorded when the current update is commited.
ZipFile has been closed.
public SetComment ( string comment ) : void
comment string The comment to record.
return void
Exemplo n.º 1
0
        public void UpdateCommentOnlyOnDisk()
        {
            string tempFile = GetTempFilePath();
            Assert.IsNotNull(tempFile, "No permission to execute this test?");

            tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");
            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);
            }

            using (ZipFile testFile = ZipFile.Create(tempFile))
            {
                testFile.BeginUpdate();
                testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("", testFile.ZipFileComment);

                testFile.BeginUpdate(new DiskArchiveStorage(testFile, FileUpdateMode.Direct));
                testFile.SetComment("Here is my comment");
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
            }
            File.Delete(tempFile);

            // Variant using indirect updating.
            using (ZipFile testFile = ZipFile.Create(tempFile))
            {
                testFile.BeginUpdate();
                testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("", testFile.ZipFileComment);

                testFile.BeginUpdate();
                testFile.SetComment("Here is my comment");
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
            }
            File.Delete(tempFile);
        }
Exemplo n.º 2
0
        public void UpdateCommentOnlyInMemory()
        {
            MemoryStream ms = new MemoryStream();

            using (ZipFile testFile = new ZipFile(ms))
            {
                testFile.IsStreamOwner = false;
                testFile.BeginUpdate();
                testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(ms))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("", testFile.ZipFileComment);
                testFile.IsStreamOwner = false;

                testFile.BeginUpdate();
                testFile.SetComment("Here is my comment");
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(ms))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
            }
        }
Exemplo n.º 3
0
 private void saveNotes_Click(object sender, EventArgs e)
 {
     try
     {
         ZipFile zf = new ZipFile(jarList.SelectedNode.Name);
         zf.BeginUpdate();
         zf.SetComment(jarCommentBox.Text);
         zf.CommitUpdate();
         zf.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, Util.langNode("couldnotsavenotes") + " " + ex.Message + "\n" + ex.StackTrace, Util.langNode("errorwhilesavingnotes"), MessageBoxButtons.OK);
     }
 }
Exemplo n.º 4
0
        private void groupBox1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                modInstallerLabel.Text = Util.langNode("adding");
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                ZipFile zf = new ZipFile(jarList.SelectedNode.Name);
                String comment = zf.ZipFileComment + "\r\n--- " + DateTime.Now + " " + Util.langNode("addedthesefiles") + " ---\r\n";
                zf.BeginUpdate();
                int i = 0;
                foreach (string file in files)
                {
                    Console.WriteLine("Adding " + file);
                    if (((File.GetAttributes(file) & FileAttributes.Directory) == FileAttributes.Directory))
                    {

                        System.Console.WriteLine("It is a directory!");
                        //zf.AddDirectory(dirname);
                        string[] filesindir = Directory.GetFiles(file, "*", SearchOption.AllDirectories);

                        // Display all the files.
                        foreach (string fileindir in filesindir)
                        {
                            Console.WriteLine("Adding " + fileindir.Replace(Directory.GetParent(file).FullName, ""));
                            zf.Add(fileindir, fileindir.Replace(Directory.GetParent(file).FullName, ""));
                            comment += fileindir.Replace(Directory.GetParent(file).FullName, "") + "\r\n";
                            i++;
                        }

                    }
                    else
                    {
                        zf.Add(file, System.IO.Path.GetFileName(file));
                        comment += System.IO.Path.GetFileName(file) + "\r\n";
                        i++;
                    }
                }
                ZipEntry mi = zf.GetEntry("META-INF");
                if (mi != null)
                {
                    zf.Delete(mi);
                    comment += String.Format(Util.langNode("removedfile"), "META-INF") + "\r\n";
                }
                zf.SetComment(comment);
                jarCommentBox.Text = comment;
                zf.CommitUpdate();
                zf.Close();
                modInstallerLabel.Text = String.Format(Util.langNode("addedfiles"), i);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Could not write: " + ex.Message + "\r\n" + ex.StackTrace);
                modInstallerLabel.Text = String.Format(Util.langNode("errordetail"), ex.Message);
            }

        }
Exemplo n.º 5
0
        public async Task <IActionResult> Download(string ids)
        {
            var fileDir = _webEnvironment.WebRootPath;
            var itemIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (itemIds.Length == 1)
            {
                if (!Guid.TryParse(itemIds[0], out Guid id))
                {
                    return(new EmptyResult());
                }
                var resume = await _resumeManager.FindByIdAsync(id);

                if (resume == null)
                {
                    return(new EmptyResult());
                }
                if (resume.Attachments == null || resume.Attachments.Count == 0)
                {
                    return(new EmptyResult());
                }
                var attachment = resume.Attachments.FirstOrDefault();

                var extensionName = attachment.FilePath.Substring(attachment.FilePath.LastIndexOf("."));
                // 命名新的文件 “姓名_职位.文件后缀名”
                var newFileName = resume.Name;
                var job         = await _jobManager.FindByIdAsync(resume.JobId);

                if (job != null)
                {
                    newFileName += $"_{job.Title}";
                }
                Response.Headers.Add("Content-Disposition", $"attachment;filename={HttpUtility.UrlEncode($"{newFileName}{extensionName}")}");

                // 文件的实际路径
                var filePath = $"{fileDir}{attachment.FilePath}";

                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    using (Response.Body)
                    {
                        int  bufferSize    = 1024;
                        long contentLength = fileStream.Length;
                        Response.ContentLength = contentLength;

                        byte[] buffer;
                        long   hasRead = 0;
                        while (hasRead < contentLength)
                        {
                            if (HttpContext.RequestAborted.IsCancellationRequested)
                            {
                                break;
                            }
                            buffer = new byte[bufferSize];
                            int currentRead = fileStream.Read(buffer, 0, bufferSize);
                            await Response.Body.WriteAsync(buffer, 0, currentRead);

                            Response.Body.Flush();
                            hasRead += currentRead;
                        }
                        Response.Body.Close();
                    }
                }
            }
            else if (itemIds.Length > 1)
            {
                var tempDir = $"{fileDir}/download/{DateTime.Now:yyyyMMddHH}";
                if (Directory.Exists(tempDir))
                {
                    Directory.Delete(tempDir, true);
                }
                Directory.CreateDirectory(tempDir);

                var zipFilePath = $"{tempDir}/{DateTime.Now:yyyyMMddHHmmssfff}.zip";
                ZipConstants.DefaultCodePage = Encoding.GetEncoding("UTF-8").CodePage;
                using (ICSharpCode.SharpZipLib.Zip.ZipFile zip = ICSharpCode.SharpZipLib.Zip.ZipFile.Create(zipFilePath))
                {
                    zip.BeginUpdate();
                    zip.SetComment("简历");
                    foreach (var itemId in itemIds)
                    {
                        if (!Guid.TryParse(itemId, out Guid id))
                        {
                            continue;
                        }
                        var resume = await _resumeManager.FindByIdAsync(id);

                        if (resume == null)
                        {
                            continue;
                        }
                        if (resume.Attachments == null || resume.Attachments.Count == 0)
                        {
                            continue;
                        }

                        var attachment = resume.Attachments.FirstOrDefault();
                        // 文件的实际路径
                        var filePath      = $"{fileDir}{attachment.FilePath}";
                        var extensionName = filePath.Substring(filePath.LastIndexOf("."));
                        // 命名新的文件 “姓名_职位.文件后缀名”
                        var newFileName = resume.Name;
                        var job         = await _jobManager.FindByIdAsync(resume.JobId);

                        if (job != null)
                        {
                            newFileName += $"_{job.Title}";
                        }
                        // 复制到压缩目录
                        var newFilePath = $"{tempDir}/{newFileName}{extensionName}";
                        IOFile.Copy(filePath, newFilePath, true);

                        zip.Add(newFilePath, $"{newFileName}{extensionName}");
                    }
                    zip.CommitUpdate();
                }

                using (FileStream fileStream = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read))
                {
                    var buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    return(File(buffer, "application/zip", zipFilePath.Substring(zipFilePath.LastIndexOf("/") + 1)));
                }
            }
            return(new EmptyResult());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generates the image.
        /// </summary>
        /// <remarks>CFI, 2012-02-24</remarks>
        private void GenerateImage()
        {
            #region Check parameter:

            if (!Directory.Exists(textBoxInputFilePath.Text))
                throw new DirectoryNotFoundException("The input directory was not found!");
            if (!Directory.Exists(Path.GetDirectoryName(textBoxOutputFile.Text)))
                throw new DirectoryNotFoundException("The output path is invalid!");
            if (textBoxStickName.Text.Length > 11 || textBoxStickName.Text == string.Empty)
                throw new ArgumentException("Invalid Stick name!");
            if (textBoxStickID.Text.Length != 9 || textBoxStickID.Text[4] != '-')
                throw new ArgumentException("Invalid Stick ID!");

            #endregion

            string outputFilePath = Path.ChangeExtension(textBoxOutputFile.Text, ".MLifterStick");

            #region Create Zip:

            Stream output = File.Create(outputFilePath);
            ZipOutputStream zipStream = new ZipOutputStream(output);
            ZipFile zip = new ZipFile(output);

            #endregion

            #region Fill Zip:

            zip.BeginUpdate();

            int pos = 1;
            string[] files = Directory.GetFiles(textBoxInputFilePath.Text, "*.*", SearchOption.AllDirectories);
            List<string> folders = new List<string>();
            foreach (string file in files)
            {
                toolStripStatusLabelMessage.Text = string.Format("Adding file {0} of {1}...", pos, files.Length);
                toolStripProgressBarStatus.Value = Convert.ToInt32(pos++ * 1.0 / files.Length * 100);
                Application.DoEvents();

                string dir = Path.GetDirectoryName(file).Remove(0, textBoxInputFilePath.Text.Length);
                string zipFile = Path.Combine(dir, Path.GetFileName(file));

                if (dir.Length > 0)
                {
                    if (!folders.Contains(dir))
                    {
                        zip.AddDirectory(dir);
                        folders.Add(dir);
                    }
                }
                zip.Add(new FileDataSource(file), zipFile);
            }

            toolStripStatusLabelMessage.Text = "Saving image. THIS COULD RUN VERY LONG - PLEASE WAIT!";
            Application.DoEvents();

            zip.CommitUpdate();

            #endregion

            VolumeDataStorage vds = new VolumeDataStorage();

            #region setting file attributes:

            foreach (string file in files)
            {
                string dir = Path.GetDirectoryName(file).Remove(0, textBoxInputFilePath.Text.Length);
                if (dir.StartsWith("\\"))
                    dir = dir.Remove(0, 1);
                string zipFile = Path.Combine(dir, Path.GetFileName(file)).Replace(@"\", "/");
                ZipEntry entry = zip.GetEntry(zipFile);
                if (entry == null)
                    continue;
                FileAttributes attr = (new FileInfo(file)).Attributes;
                if (!(attr == FileAttributes.Archive || attr == FileAttributes.Normal))
                    vds.FileAttributeList.Add(entry.Name, attr);
                toolStripStatusLabelMessage.Text = "Setting file FileAttributes to: " + zipFile;
            }
            foreach (string folder in folders)
            {
                FileAttributes attr = (new DirectoryInfo(Path.Combine(textBoxInputFilePath.Text, folder))).Attributes;
                if (!(attr == FileAttributes.Archive || attr == FileAttributes.Normal || attr == FileAttributes.Directory))
                    vds.FolderAttributeList.Add(folder, attr);
                toolStripStatusLabelMessage.Text = "Setting folder FileAttributes to: " + folder;
            }

            #endregion

            #region Set Comment:

            vds.VolumeLabel = textBoxStickName.Text;
            vds.VolumeSerial = textBoxStickID.Text;
            string comment = VolumeDataStorage.SerializeData(vds);

            File.WriteAllText(Path.Combine(Application.StartupPath, "Comment.txt"), comment);

            zip.BeginUpdate();
            zip.SetComment(comment);
            zip.CommitUpdate();

            #endregion

            zip.Close();

            CreatedImage = outputFilePath;
        }