Skip to content

Commit

Permalink
Fixed a Zip bug on Symbolic Links
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven-D-Foster committed Jul 16, 2021
1 parent 9ba28fa commit 965f5c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions MaxRunSoftware.Utilities.Console/Commands/Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ protected override void ExecuteInternal()
var fi = new FileInfo(includedItem);
External.Zip.AddFileToZip(fi, fi.Directory, zos, bufferSize, Path.GetFileName(outputFile), encrypt: password != null);
}
else
else // Directory
{
// Directory
var basePath = new DirectoryInfo(includedItem);
var items = Util.FileList(includedItem, recursive: recursive);
if (!recursive) items = items.Where(o => !o.IsDirectory || string.Equals(o.Path, includedItem, isWindows ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
Expand Down Expand Up @@ -143,6 +142,7 @@ protected override void ExecuteInternal()
}

zos.IsStreamOwner = true; // Makes the Close also Close the underlying stream
//zos.CloseEntry();
zos.Flush();
fs.Flush();
zos.Close();
Expand Down
7 changes: 5 additions & 2 deletions MaxRunSoftware.Utilities.External/Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ public static void AddFileToZip(FileInfo file, DirectoryInfo baseDirectoryToRemo
var newEntry = new ZipEntry(entryPath)
{
DateTime = file.LastWriteTime,
Size = file.Length
Size = file.GetLength()
};
if (encrypt) newEntry.AESKeySize = 256;
zos.PutNextEntry(newEntry);
//var buffer = new byte[bufferSize];
using (var fs = Util.FileOpenRead(file.FullName))
{
//StreamUtils.Copy(fs, zos, buffer);
fs.CopyTo(zos);
var bytes = fs.CopyToWithCount(zos);
log.Debug($"Wrote: {file.Name} ({bytes})");
}
zos.CloseEntry();
log.Info($"Added: {file.FullName} --> {zipFileName}/{entryPath}");


}

public static void AddDirectoryToZip(DirectoryInfo directory, DirectoryInfo baseDirectoryToRemove, ZipOutputStream zos, string zipFileName, bool encrypt = false)
Expand Down
36 changes: 22 additions & 14 deletions MaxRunSoftware.Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,25 @@ public static string[] RemoveBase(this FileSystemInfo info, DirectoryInfo baseTo
return list.ToArray();
}

public static long GetLength(this FileInfo file)
{
if (file == null) return -1;

// https://stackoverflow.com/a/26473940
if (file.Attributes.HasFlag(FileAttributes.ReparsePoint)) // probably symbolic link
{
// https://stackoverflow.com/a/57454136
using (Stream fs = Util.FileOpenRead(file.FullName))
{
return fs.Length;
}
}
else // regular file
{
return file.Length;
}
}

#endregion File

#region System.Net
Expand Down Expand Up @@ -2353,7 +2372,7 @@ public static long Read(this StreamReader reader, Action<char[]> action, int buf
return totalRead;
}

public static long CopyTo(this Stream source, Stream target) => CopyTo(source, target, Constant.BUFFER_SIZE_OPTIMAL);
public static long CopyToWithCount(this Stream source, Stream target) => CopyToWithCount(source, target, Constant.BUFFER_SIZE_OPTIMAL);

/// <summary>
/// Reads all the bytes from the current stream and writes them to the destination stream
Expand All @@ -2362,7 +2381,7 @@ public static long Read(this StreamReader reader, Action<char[]> action, int buf
/// <param name="source">The current stream.</param>
/// <param name="target">The stream that will contain the contents of the current stream.</param>
/// <param name="bufferSize">The size of the buffer to use.</param>
public static long CopyTo(this Stream source, Stream target, int bufferSize)
public static long CopyToWithCount(this Stream source, Stream target, int bufferSize)
{
source.CheckNotNull(nameof(source));
target.CheckNotNull(nameof(target));
Expand All @@ -2379,24 +2398,13 @@ public static long CopyTo(this Stream source, Stream target, int bufferSize)
return totalCount;
}

public static long CopyToWithCount(this Stream source, Stream target) => CopyTo(source, target);

/// <summary>
/// Reads all the bytes from the current stream and writes them to the destination stream
/// with the specified buffer size.
/// </summary>
/// <param name="source">The current stream.</param>
/// <param name="target">The stream that will contain the contents of the current stream.</param>
/// <param name="bufferSize">The size of the buffer to use.</param>
public static long CopyToWithCount(this Stream source, Stream target, int bufferSize) => CopyTo(source, target, bufferSize);

public static void WriteToFile(this Stream stream, string path, int bufferSize)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
//if (File.Exists(path)) File.Delete(path);
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, FileOptions.None))
{
CopyTo(stream, fs, bufferSize);
CopyToWithCount(stream, fs, bufferSize);
fs.Flush();
}
}
Expand Down

0 comments on commit 965f5c1

Please sign in to comment.