/* Copyright (c) Citrix Systems, Inc. * 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; using XenCenterLib.Compression; namespace XenCenterLib.Archive { /// /// A static factory to create an object that will allow the archiving of data /// public static class ArchiveFactory { /// /// Supported types of archive /// public enum Type { Tar, TarGz, TarBz2, Zip } /// /// Instantiate a class that can read a archive type /// /// Type of archive to read /// The contents of packaged data /// if there is not a iterator for a specified archive type /// ArchiveIterator to allow an archive to be traversed public static ArchiveIterator Reader(Type archiveType, Stream packagedData) { if (archiveType == Type.Tar) return new SharpZipTarArchiveIterator(packagedData); if (archiveType == Type.TarGz) return new SharpZipTarArchiveIterator(CompressionFactory.Reader(CompressionFactory.Type.Gz, packagedData)); if (archiveType == Type.TarBz2) return new SharpZipTarArchiveIterator(CompressionFactory.Reader(CompressionFactory.Type.Bz2, packagedData)); if (archiveType == Type.Zip) return new DotNetZipZipIterator(packagedData); throw new NotSupportedException(String.Format("Type: {0} is not supported by ArchiveIterator", archiveType)); } /// /// Instantiate a class that can write a archive type /// /// Type of archive to write /// The placed where the packaged data will be stored /// if there is not a writer for a specified archive type /// ArchiveWriter to allow an archive to be written public static ArchiveWriter Writer(Type archiveType, Stream targetPackage) { if (archiveType == Type.Tar) return new SharpZipTarArchiveWriter(targetPackage); if (archiveType == Type.Zip) return new DotNetZipZipWriter(targetPackage); throw new NotSupportedException( String.Format( "Type: {0} is not supported by ArchiveWriter", archiveType ) ); } } }