Click here to Skip to main content
15,896,207 members
Articles / General Programming / Threads

SimplePack Library

Rate me:
Please Sign up or sign in to vote.
4.93/5 (30 votes)
23 Nov 2010CPOL7 min read 73.1K   1.9K   97  
Simple way how to pack data into one file
using System;
using System.IO;
using System.Xml.Serialization;
using SimplePack.Helper;

namespace SimplePack.Data
{
    ///<summary>
    /// Abstract class contain basic information which should has all entities of Archive (File, Directory) except Footer.
    ///</summary>
    [Serializable]
    public abstract class ElementaryArchive
    {
        private string name;
        private string fullPath;
        private IArchiveStructure parentDirectory;
        private long length;   //aka size
        private bool isReadOnly;
        private bool isHidden;
        private bool isSystem;
        private bool isArchive;
        private bool isCompressed;
        private bool isEncrypted;

        ///<summary>
        /// Name of entity.
        ///</summary>
        [XmlAttribute]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        ///<summary>
        /// Full path of entity.
        /// <example>For directory: arch:\nameOfDirectory\
        /// For file: arch:\nameOfFile</example>
        ///</summary>
        [XmlAttribute]
        public string FullPath
        {
            get { return fullPath; }
            set { fullPath = value; }
        }

        ///<summary>
        /// ParentDirectory points to parent directory.
        ///</summary>
        [XmlIgnore]
        public IArchiveStructure ParentDirectory
        {
            get { return parentDirectory; }
            set { parentDirectory = value; }
        }

        ///<summary>
        /// Length holds size of entity.
        ///</summary>
        [XmlAttribute]
        public long Length
        {
            get { return length; }
            set { length = value; }
        }

        ///<summary>
        /// <see langword="true"/> if file/directory is read-only, otherwise <see langword="false"/>
        ///</summary>
        [XmlAttribute(AttributeName = "r")]
        public bool IsReadOnly
        {
            get { return isReadOnly; }
            set { isReadOnly = value; }
        }

        ///<summary>
        /// <see langword="true"/> if file/directory is hidden, otherwise <see langword="false"/>
        ///</summary>
        [XmlAttribute(AttributeName = "h")]
        public bool IsHidden
        {
            get { return isHidden; }
            set { isHidden = value; }
        }

        ///<summary>
        /// <see langword="true"/> if file/directory is system, otherwise <see langword="false"/>
        ///</summary>
        [XmlAttribute(AttributeName = "s")]
        public bool IsSystem
        {
            get { return isSystem; }
            set { isSystem = value; }
        }

        ///<summary>
        /// <see langword="true"/> if file/directory is archive, otherwise <see langword="false"/>
        ///</summary>
        [XmlAttribute(AttributeName = "a")]
        public bool IsArchive
        {
            get { return isArchive; }
            set { isArchive = value; }
        }

        ///<summary>
        /// <see langword="true"/> if file/directory is compressed, otherwise <see langword="false"/>
        ///</summary>
        [XmlAttribute(AttributeName = "c")]
        public bool IsCompressed
        {
            get { return isCompressed; }
            set { isCompressed = value; }
        }

        ///<summary>
        /// <see langword="true"/> if file/directory is encrypted, otherwise <see langword="false"/>
        ///</summary>
        [XmlAttribute(AttributeName = "e")]
        public bool IsEncrypted
        {
            get { return isEncrypted; }
            set { isEncrypted = value; }
        }

        ///<summary>
        /// Setup <see cref="ElementaryArchive"/> attributes according <see cref="FileAttributes"/> 
        ///</summary>
        ///<param name="attributes">File/Directory attributes</param>
        public void ApplyFileAttributeOnElement(FileAttributes attributes)
        {
            IsReadOnly = ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            IsHidden = ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
            IsSystem = ((attributes & FileAttributes.System) == FileAttributes.System);
            IsArchive = ((attributes & FileAttributes.Archive) == FileAttributes.Archive);
            IsCompressed = ((attributes & FileAttributes.Compressed) == FileAttributes.Compressed);
            IsEncrypted = ((attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted);
        }

        ///<summary>
        /// Apply archiveFile/archiveDirectory settings on file system file or directory.
        ///</summary>
        ///<param name="path">File or Directory path on HD</param>
        public void ApplyFileAttributeOnFileSystem(string path)
        {
            FileAttributes attributes = 0;
            if (IsReadOnly)
                attributes = attributes | FileAttributes.ReadOnly;
            if (IsHidden)
                attributes = attributes | FileAttributes.Hidden;
            if (IsSystem)
                attributes = attributes | FileAttributes.System;
            if (IsArchive)
                attributes = attributes | FileAttributes.Archive;
            if (IsCompressed)
                attributes = attributes | FileAttributes.Compressed;
            if (IsEncrypted)
                attributes = attributes | FileAttributes.Encrypted;
            File.SetAttributes(path, attributes);
        }

        ///<summary>
        /// Concatenate value of Name and <see cref="FullPath"/>.
        ///</summary>
        ///<returns>Concatenate value of Name and <see cref="FullPath"/></returns>
        public override string ToString()
        {
            return string.Format("Name: {0}, FullPath: {1}", Name, FullPath);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions