-
Notifications
You must be signed in to change notification settings - Fork 3
Hierarchy support in Zip task #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,28 +4,40 @@ | |
| <UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> | ||
| <ParameterGroup> | ||
| <InputFileNames ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> | ||
| <InputBaseDirectory ParameterType="System.String" Required="false" /> | ||
| <OutputFileName ParameterType="System.String" Required="true" /> | ||
| <OverwriteExistingFile ParameterType="System.Boolean" Required="false" /> | ||
| </ParameterGroup> | ||
| </ParameterGroup> | ||
| <Task> | ||
| <Reference Include="System.IO.Compression" /> | ||
| <Using Namespace="System.IO.Compression" /> | ||
| <Code Type="Fragment" Language="cs"> | ||
| <![CDATA[ | ||
| <![CDATA[ | ||
| Log.LogMessage(MessageImportance.High, "=== Zipping files ==="); | ||
|
|
||
| var fileMode = OverwriteExistingFile ? FileMode.Create : FileMode.CreateNew; | ||
| using (var archive = new ZipArchive(new FileStream(OutputFileName, fileMode), ZipArchiveMode.Create)) | ||
| var baseDir = string.IsNullOrWhiteSpace(InputBaseDirectory) ? string.Empty : Path.GetFullPath(InputBaseDirectory); | ||
| var filesToZip = InputFileNames.Select(f => Path.GetFullPath(f.ItemSpec)).Where(n => n.StartsWith(baseDir)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would the user provided a collection containing files they don't want to zip? The idea is that |
||
|
|
||
| using (var archive = new ZipArchive(new FileStream(OutputFileName, fileMode), ZipArchiveMode.Create, leaveOpen: false)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this as you asked this question on my first pull request, so other people would not have to ask this question again or check the documentation to clearify this.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I remember the conversation. I was wrong. I don't think it adds any value. Sorry! |
||
| { | ||
| foreach (var inputFileName in InputFileNames.Select(f => f.ItemSpec)) | ||
| foreach (var inputFileName in filesToZip) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please use 4 space indent
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pardon?! Is "nit" in Polish or in English? What do you mean?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit = nitpick. we use it at work for small/cosmetic issues |
||
| { | ||
| var entryName = baseDir.Length == 0 | ||
| ? Path.GetFileName(inputFileName) | ||
| : inputFileName.Substring(baseDir.Length + 1); | ||
|
|
||
| Log.LogMessage(MessageImportance.High, "Zipping '" + entryName + "' from " + inputFileName); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why this is useful. This will just clutter the logs especially where there is a lot of files to zip. |
||
|
|
||
| var archiveEntry = archive.CreateEntry(entryName); | ||
| using (var fs = new FileStream(inputFileName, FileMode.Open)) | ||
| { | ||
| var archiveEntry = archive.CreateEntry(Path.GetFileName(inputFileName)); | ||
| using (var fs = new FileStream(inputFileName, FileMode.Open)) | ||
| { | ||
| using (var zipStream = archiveEntry.Open()) | ||
| { | ||
| fs.CopyTo(zipStream); | ||
| } | ||
| } | ||
| using (var zipStream = archiveEntry.Open()) | ||
| { | ||
| fs.CopyTo(zipStream); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ]]> | ||
| </Code> | ||
|
|
@@ -38,7 +50,7 @@ | |
| <OutputFileName ParameterType="System.String" Required="true" /> | ||
| <OverwriteExistingFile ParameterType="System.Boolean" Required="false" /> | ||
| <IncludeBaseDirectory ParameterType="System.Boolean" Required="false" /> | ||
| </ParameterGroup> | ||
| </ParameterGroup> | ||
| <Task> | ||
| <Reference Include="System.IO.Compression" /> | ||
| <Reference Include="System.IO.Compression.FileSystem" /> | ||
|
|
@@ -47,16 +59,16 @@ | |
| <![CDATA[ | ||
| if (File.Exists(OutputFileName)) | ||
| { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no changes below apart from formatting. Please revert. |
||
| if (!OverwriteExistingFile) | ||
| { | ||
| return false; | ||
| } | ||
| File.Delete(OutputFileName); | ||
| if (!OverwriteExistingFile) | ||
| { | ||
| return false; | ||
| } | ||
| File.Delete(OutputFileName); | ||
| } | ||
| ZipFile.CreateFromDirectory | ||
| ( | ||
| InputBaseDirectory, OutputFileName, | ||
| CompressionLevel.Optimal, IncludeBaseDirectory | ||
| InputBaseDirectory, OutputFileName, | ||
| CompressionLevel.Optimal, IncludeBaseDirectory | ||
| ); | ||
| ]]> | ||
| </Code> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove - I don't think it is useful. The logs will contain the name of the target being invoked. The user can also do this in their code if they find it useful.