2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace XenCenterLib.Archive
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A base abstract class to iterate over an archived file type
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class ArchiveIterator : IDisposable
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper function to extract all contents of this iterating class to a path
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pathToExtractTo">The path to extract the archive to</param>
|
2020-09-29 15:28:52 +02:00
|
|
|
|
/// <param name="cancellingDelegate"></param>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <exception cref="ArgumentNullException">If null path is passed in</exception>
|
|
|
|
|
/// <exception cref="NullReferenceException">If while combining path and current file name a null arises</exception>
|
2020-09-29 15:28:52 +02:00
|
|
|
|
public void ExtractAllContents(string pathToExtractTo, Func<bool> cancellingDelegate = null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-09-29 15:28:52 +02:00
|
|
|
|
if (String.IsNullOrEmpty(pathToExtractTo))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
|
2020-09-29 15:28:52 +02:00
|
|
|
|
while (HasNext())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-09-29 15:28:52 +02:00
|
|
|
|
if (cancellingDelegate != null && cancellingDelegate())
|
|
|
|
|
throw new OperationCanceledException();
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
//Make the file path from the details in the archive making the path windows friendly
|
|
|
|
|
string conflatedPath = Path.Combine(pathToExtractTo, CurrentFileName()).Replace('/', Path.DirectorySeparatorChar);
|
2020-09-29 15:28:52 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
//Create directories - empty ones will be made too
|
2020-09-29 15:28:52 +02:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(conflatedPath));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
//If we have a file extract the contents
|
2020-09-29 15:28:52 +02:00
|
|
|
|
if (!IsDirectory())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
using (FileStream fs = File.Create(conflatedPath))
|
|
|
|
|
{
|
2020-09-29 15:28:52 +02:00
|
|
|
|
ExtractCurrentFile(fs);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// Hook to allow the base stream to be wrapped by this class's archive mechanism
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream">base stream</param>
|
|
|
|
|
public virtual void SetBaseStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public virtual bool VerifyCurrentFileAgainstDigest(string algorithmName, byte[] digest)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public abstract bool HasNext();
|
|
|
|
|
public abstract void ExtractCurrentFile(Stream extractedFileContents);
|
|
|
|
|
public abstract string CurrentFileName();
|
|
|
|
|
public abstract long CurrentFileSize();
|
|
|
|
|
public abstract DateTime CurrentFileModificationTime();
|
|
|
|
|
public abstract bool IsDirectory();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dispose hook - overload and clean up IO
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="disposing"></param>
|
2020-09-29 15:28:52 +02:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2020-09-29 15:28:52 +02:00
|
|
|
|
GC.SuppressFinalize(this);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|