Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Add possibilty to clone a zip entry to add it to a different zip
Browse files Browse the repository at this point in the history
  • Loading branch information
romerod committed Oct 19, 2020
1 parent 770d60d commit 6944eb8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Zip.Shared/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,76 @@ private static ZipEntry Create(string nameInArchive, ZipEntrySource source, Obje
}


public ZipEntry CloneForNewZipFile(ZipFile newZipFile)
{
if (this.Source != ZipEntrySource.ZipFile)
{
throw new InvalidOperationException("The entry you are trying to add wasn't loaded from a zip file.");
}

if (this.IsChanged)
{
throw new InvalidOperationException("The entry you are trying to add was modified.");
}

if (this.IsDirectory)
{
throw new InvalidOperationException("The entry you are trying to add is a directory.");
}

if (!this.CompressionMethod.Equals(newZipFile.CompressionMethod))
{
throw new InvalidOperationException("The entry you are trying to add uses another compression method.");
}

if (this.ArchiveStream is ZipSegmentedStream)
{
throw new InvalidOperationException("The entry you are trying to add is from a multi-part zip.");
}

var clone = new ZipEntry
{
__FileDataPosition = this.__FileDataPosition,
_actualEncoding = this._actualEncoding,
_archiveStream = this._archiveStream,
_Atime = this._Atime,
_Comment = this._Comment,
_CompressedSize = this._CompressedSize,
_CompressionLevel = this._CompressionLevel,
_CompressionMethod = this._CompressionMethod,
_Crc32 = this._Crc32,
_Ctime = this._Ctime,
_emitNtfsTimes = this._emitNtfsTimes,
_emitUnixTimes = this._emitUnixTimes,
_Encryption = this._Encryption,
_Encryption_FromZipFile = this._Encryption_FromZipFile,
_entryRequiresZip64 = this._entryRequiresZip64,
_FileNameInArchive = this._FileNameInArchive,
_LastModified = this._LastModified,
_LengthOfHeader = this._LengthOfHeader,
_metadataChanged = this._metadataChanged,
_Mtime = this._Mtime,
_ntfsTimesAreSet = this._ntfsTimesAreSet,
_Password = this._Password,
_presumeZip64 = this._presumeZip64,
_RelativeOffsetOfLocalHeader = this._RelativeOffsetOfLocalHeader,
_restreamRequiredOnSave = this._restreamRequiredOnSave,
_sourceIsEncrypted = this._sourceIsEncrypted,
_TimeBlob = this._TimeBlob,
_timestamp = this._timestamp,
_UncompressedSize = this._UncompressedSize,
_VersionMadeBy = this._VersionMadeBy,
_VersionNeeded = this._VersionNeeded,
AlternateEncoding = this.AlternateEncoding,
AlternateEncodingUsage = this.AlternateEncodingUsage,
ExtractExistingFile = this.ExtractExistingFile,
SetCompression = this.SetCompression,
ZipErrorAction = this.ZipErrorAction,
_Source = this._Source
};

return clone;
}

internal void MarkAsDirectory()
{
Expand Down
10 changes: 10 additions & 0 deletions src/Zip.Shared/ZipFile.AddUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,17 @@ public ZipEntry AddEntry(string entryName, OpenDelegate opener, CloseDelegate cl
return _InternalAddEntry(ze);
}

public void AddEntry(ZipEntry ze)
{
if (ze._container != null)
{
throw new InvalidOperationException("Entry already belongs to a zip file");
}

ze._container = new ZipContainer(this);
InternalAddEntry(ze.FileName, ze);
AfterAddEntry(ze);
}

private ZipEntry _InternalAddEntry(ZipEntry ze)
{
Expand Down

0 comments on commit 6944eb8

Please sign in to comment.