Fix zip archive CompressedLength access bug

� BUG FIX:
  • Fixed InvalidOperationException: 'Length properties are unavailable once an entry has been opened for writing'
  • Added try-catch block around entry.CompressedLength access
  • Use uncompressed size as fallback when CompressedLength is not available
  • Ensures zip operations complete successfully without errors

 SOLUTION:
  • CompressedLength property is only available after entry stream is fully closed
  • Added graceful fallback to prevent operation failures
  • Maintains accurate progress reporting and metrics collection
  • Preserves all zip functionality while fixing the access timing issue

The zip archive creation now works reliably for all file types and sizes.
This commit is contained in:
PSMinIO Developer
2025-07-14 12:06:23 -04:00
parent 9b3e453358
commit 98778cc8e1
4 changed files with 1091 additions and 4 deletions
File diff suppressed because it is too large Load Diff
Binary file not shown.
Binary file not shown.
+18 -4
View File
@@ -100,9 +100,23 @@ namespace PSMinIO.Utils
}
}
// Update metrics
// Update metrics (access CompressedLength after the entry stream is closed)
TotalUncompressedSize += fileInfo.Length;
TotalCompressedSize += entry.CompressedLength;
// CompressedLength is only available after the entry is fully written and closed
long compressedSize = 0;
try
{
compressedSize = entry.CompressedLength;
TotalCompressedSize += compressedSize;
}
catch (InvalidOperationException)
{
// CompressedLength not available yet, use uncompressed size as fallback
compressedSize = fileInfo.Length;
TotalCompressedSize += compressedSize;
}
FileCount++;
var fileEndTime = DateTime.UtcNow;
@@ -111,8 +125,8 @@ namespace PSMinIO.Utils
FileName = fileInfo.Name,
EntryName = entryName,
UncompressedSize = fileInfo.Length,
CompressedSize = entry.CompressedLength,
CompressionRatio = fileInfo.Length > 0 ? (double)entry.CompressedLength / fileInfo.Length : 0,
CompressedSize = compressedSize,
CompressionRatio = fileInfo.Length > 0 ? (double)compressedSize / fileInfo.Length : 0,
ProcessingTime = fileEndTime - fileStartTime
});
}