Skip to content

Commit 5f57a54

Browse files
committed
Z-1684 Fixed File Timestamp being lost via ZipFile.Add with modified entryname
1 parent 9322513 commit 5f57a54

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/Zip/IEntryFactory.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
// obligated to do so. If you do not wish to do so, delete this
3636
// exception statement from your version.
3737

38+
// HISTORY
39+
// 2012-11-29 Z-1684 Added MakeFileEntry(string fileName, string entryName, bool useFileSystem)
40+
3841
using ICSharpCode.SharpZipLib.Core;
3942

4043
namespace ICSharpCode.SharpZipLib.Zip
@@ -59,6 +62,15 @@ public interface IEntryFactory
5962
/// <returns>Returns a <see cref="ZipEntry">file entry</see> based on the <paramref name="fileName"/> passed.</returns>
6063
ZipEntry MakeFileEntry(string fileName, bool useFileSystem);
6164

65+
/// <summary>
66+
/// Create a <see cref="ZipEntry"/> for a file given its actual name and optional override name
67+
/// </summary>
68+
/// <param name="fileName">The name of the file to create an entry for.</param>
69+
/// <param name="entryName">An alternative name to be used for the new entry. Null if not applicable.</param>
70+
/// <param name="useFileSystem">If true get details from the file system if the file exists.</param>
71+
/// <returns>Returns a <see cref="ZipEntry">file entry</see> based on the <paramref name="fileName"/> passed.</returns>
72+
ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSystem);
73+
6274
/// <summary>
6375
/// Create a <see cref="ZipEntry"/> for a directory given its name
6476
/// </summary>

src/Zip/ZipEntryFactory.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
// obligated to do so. If you do not wish to do so, delete this
3636
// exception statement from your version.
3737

38+
// HISTORY
39+
// 2012-11-29 Z-1684 Added MakeFileEntry(string fileName, string entryName, bool useFileSystem)
40+
3841
using System;
3942
using System.IO;
4043

@@ -207,18 +210,29 @@ public bool IsUnicodeText
207210
/// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
208211
public ZipEntry MakeFileEntry(string fileName)
209212
{
210-
return MakeFileEntry(fileName, true);
213+
return MakeFileEntry(fileName, null, true);
214+
}
215+
216+
/// <summary>
217+
/// Make a new <see cref="ZipEntry"/> for a file.
218+
/// </summary>
219+
/// <param name="fileName">The name of the file to create a new entry for.</param>
220+
/// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
221+
/// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
222+
public ZipEntry MakeFileEntry(string fileName, bool useFileSystem) {
223+
return MakeFileEntry(fileName, null, useFileSystem);
211224
}
212225

213226
/// <summary>
214227
/// Make a new <see cref="ZipEntry"/> from a name.
215228
/// </summary>
216229
/// <param name="fileName">The name of the file to create a new entry for.</param>
230+
/// <param name="entryName">An alternative name to be used for the new entry. Null if not applicable.</param>
217231
/// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
218232
/// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
219-
public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
233+
public ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSystem)
220234
{
221-
ZipEntry result = new ZipEntry(nameTransform_.TransformFile(fileName));
235+
ZipEntry result = new ZipEntry(nameTransform_.TransformFile(entryName != null && entryName.Length > 0 ? entryName : fileName));
222236
result.IsUnicodeText = isUnicodeText_;
223237

224238
int externalAttributes = 0;

src/Zip/ZipFile.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
// 2009-12-22 Z-1649 Added AES support
4242
// 2010-03-02 Z-1650 Fixed updating ODT archives in memory. Exposed exceptions in updating.
4343
// 2010-05-25 Z-1663 Fixed exception when testing local header compressed size of -1
44+
// 2012-11-29 Z-1684 Fixed ZipFile.Add(string fileName, string entryName) losing the file TimeStamp
4445

4546
using System;
4647
using System.Collections;
@@ -1647,7 +1648,7 @@ public void Add(string fileName, string entryName)
16471648
}
16481649

16491650
CheckUpdating();
1650-
AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(entryName)));
1651+
AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(fileName, entryName, true)));
16511652
}
16521653

16531654

0 commit comments

Comments
 (0)