Click here to Skip to main content
15,891,723 members
Articles / Desktop Programming / Windows Forms

Listing and Working with Files in Archives

Rate me:
Please Sign up or sign in to vote.
2.86/5 (6 votes)
22 Jun 2007CPOL 32.3K   529   23  
This article describes how to use CAKE3, which is a wrapper component for many archiver DLLs,
using System;
using System.Runtime.InteropServices;

namespace AceDllWrapped
{
	/// <summary>
	/// 
	/// Functions and their structures for Ace-Dll and UnAce-Dll.
	/// 
	/// </summary>

	//--------------------------------------------------------------------------------
	//-------=====---  Part 4: Functions supported by the AceV2.Dll ---====-----------
	//--------------------------------------------------------------------------------

	//---------------------------------------------------------------------------
	//
	// AceV2.Dll function support goes beyond UnAceV2.Dll by these functions:
	//
	//   ACEDelete
	//   ACEAdd
	//   ACEReadSFXData
	//   ACERepair
	//   ACESetComments
	//   ACEEncryptFiles
	//   ACEAddSFX
	//   ACELock
	//   ACEAddRecoveryRecord
	//   ACEAddAV                (not supported in ACL)
	//   ACERegister             (not supported in ACL)
	//   ACEShowRegisterDialog   (not supported in ACL)
	//
	// Look up on UNACE.H for information about Dll initialization and function
	// return codes!
	//
	//---------------------------------------------------------------------------
	//
	// Contents:
	//   Part 4.1: TimeSet defines
	//   Part 4.2: SFX module name defines
	//   Part 4.3: functions and parameter structures
	//
	//---------------------------------------------------------------------------

	public class AceFnc
	{
		public AceFnc() {}

		//---------------------------------------------------------------------------
		//=====================---  Part 4.1: TimeSet defines  ---===================
		//---------------------------------------------------------------------------
		// Set the TimeSet fields to one of these values.
		//---------------------------------------------------------------------------

		public const int ACE_SETTIME_MODIFY      = 0;  // set archive time to current system time
		public const int ACE_SETTIME_KEEP        = 1;  // keep archive time
		public const int ACE_SETTIME_LASTFILE    = 2;  // set archive time to time of freshest file in archive


		//---------------------------------------------------------------------------
		//=================---  Part 4.2: SFX module name defines  ---===============
		//---------------------------------------------------------------------------

		//--------------------------  SFX module defines  -------------------------
		// Specify a SFX module name to add an SFX using ACEAddSFX() or ACEAdd().
		// (SFX-file name/ACE_SFXMODULE_NONE/ACE_SFXMODULE_DEFAULT)
		// (see description of these constants)
		//-------------------------------------------------------------------------

		public const string ACE_SFX_NONE           = "none";     // do not add an SFX
		public const string ACE_SFX_DEFAULT        = "default";  // add defaul SFX (DOS-SFX with LFN support)


		//---------------------------------------------------------------------------
		//============---  Part 4.3: functions and parameter structures ---==========
		//---------------------------------------------------------------------------

		//---------------------------  ACEDelete  ---------------------------------
		// Remove files from archive.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACEDelete() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEDeleteStruc
		{
			public sACEFilesStruc Files;		// specifies files to delete; see tACEFilesStruc structure

			public string DecryptPassword;		// password for decryption (if files are encrypted);
												// zero-terminated string, case-sensitive (maxlen=56)

			public sACECompressParamsStruc		// compression parameters needed at solid
				CompressParams;					// archives only (for repacking of files)

			public uint TimeSet;				// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[] Reserved;				// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACEDelete() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEDelete(string ArchiveName, ref sACEDeleteStruc Delete);


		//----------------------------  ACEAdd  -----------------------------------
		// Add files to an archive. Create new archive if it does not exist.
		// If MainCmt is non-zero, then a comment is taken from CmtBuf at
		// creation to become the main comment. This function can work in
		// different modes: normal add, move, update, fresh
		// (See sACEAddStruc.AddMode)
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// Tells ACEAdd() how to perform the add operation.
		//-----------------------------------------------------------------------

        public const int  ACE_ADD_MODE_ADD       = 0;  // normal add operation
        public const int  ACE_ADD_MODE_MOVE      = 1;  // move files to archive (delete after add)
        public const int  ACE_ADD_MODE_FRESH     = 2;  // add modified files only
        public const int  ACE_ADD_MODE_UPDATE    = 3;  // add modified and new files only


		//-----------------------------------------------------------------------
		// Volume size defines.
		//-----------------------------------------------------------------------

        public const int  ACE_VOLUME_NONE        = -1; // do not create a multi volume archive
        public const int  ACE_VOLUME_AUTO        = -2; // determine volume size by space on destination disk


		//-----------------------------------------------------------------------
		// Defines how the paths of archived files are stored in the archive.
		//---------------------------------------------------------------------------

        public const int  ACE_SAVEPATH_RECURSED  = 0;  // do not save the part of the path specified in the file list
        public const int  ACE_SAVEPATH_NONE      = 1;  // do not save any path (exclude)
        public const int  ACE_SAVEPATH_FULL      = 2;  // save full path without drive
        public const int  ACE_SAVEPATH_DRIVE     = 3;  // save full path with drive letter


		//-----------------------------------------------------------------------
		// ACEAdd() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEAddStruc
		{
			public sACEFilesStruc Files;			// specifies files to add; see tACEFilesStruc structure

			public string DestinationDir;			// archive directory to add files to

			public int Mode;						// see ACE_ADD_MODE defines
			public int SavePath;					// see ACE_SAVEPATH defines

			public string EncryptPassword;			// encryption and decryption passwords,
			public string DecryptPassword;			// case-sensitive (maxlen=56)

			public sACECompressParamsStruc			// compression parameters
				CompressParams;

			public bool 
				MainComment,				// add main comment at archive creation (use the comment buffer you initialized at ACEInitDll())
				Solid,						// create solid archives
				Lock,						// lock archive after creation
				RecoveryRecord,				// add recovery record after creation
				AV;							// add AV to archive (if registered) not used in ACL

			public long VolumeSize;			// ACE_VOLUME_NONE, ACE_VOLUME_AUTO or any other value being treated as absolute volume size in bytes

			public string SFXName;			// set SFXName to ACE_SFX_NONE if you do not want to add a SFX

			public uint TimeSet;            // see ACE_SETTIME constants

			public bool  SaveNTFSSecurity;  // save NTFS security data
			public bool  SaveNTFSStreams;   // save NTFS streams

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=56)]
			byte[]  Reserved;				// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACEAdd() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEAdd(string ArchiveName, ref sACEAddStruc Add);


		//-------------------------  ACEReadSFXData  ------------------------------
		// Retrieves information from a SFX module
		// (stored in *.SFX files or default SFX).
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// Constants to be used for the LanguageIndex field of
		// tACEReadSFXDataStruc.
		//-----------------------------------------------------------------------

        public const int  ACE_READSFXDATA_ENGLISH        = 0;
        public const int  ACE_READSFXDATA_GERMAN         = 1;
        public const int  ACE_READSFXDATA_DUTCH          = 2;
        public const int  ACE_READSFXDATA_SPANISH        = 3;
        public const int  ACE_READSFXDATA_RUSSIAN        = 4;
        public const int  ACE_READSFXDATA_POLISH         = 5;
        public const int  ACE_READSFXDATA_PORTUGUESE     = 6;
        public const int  ACE_READSFXDATA_FINNISH        = 7;
        public const int  ACE_READSFXDATA_ROMANIAN       = 8;
        public const int  ACE_READSFXDATA_CZECH			 = 9;


		//-----------------------------------------------------------------------
		// ACEReadSFXData() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEReadSFXDataStruc
		{
			public uint   LanguageIndex;        // specifies which language to read out of the SFX Header (titles, description)
			
			public string 
				ShortTitle,						// returns short title of SFX module (maxlen=9)
				MediumTitle,					// returns medium title of SFX module (maxlen=23)
				LongTitle,						// returns long title of SFX module (maxlen=45)
				Description;					// returns description of SFX module (maxlen=513)

			public uint   Start;                // returns start of module within the file
			public uint   Size;                 // returns size of the module
			
			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;         // has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACEReadSFXData() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEReadSFXData(string SFXName, ref sACEReadSFXDataStruc SFXData);


		//----------------------------  ACERepair  --------------------------------
		// Recover files of a (broken) archive. If there is a recovery record -
		// it is very likely that the archive can be fully repaired.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACERepair() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACERepairStruc
		{
			public string RepairedArchiveName;  // file name of repaired archive

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;					// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACERepair() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACERepair(string ArchiveName, ref sACERepairStruc Repair);


		//-------------------------  ACESetComments  ------------------------------
		// Sets the comment(s) of an archive. If the FileList field has been set
		// to NULL or no file has been specified, the main comment is going to be
		// modified - otherwhise the comments of the specified files.
		// The comments may be changed at StateCallbackProc.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACESetComments() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACESetCommentsStruc
		{
			public sACEFilesStruc       // files to set comments of; see tACEFilesStruc structure;
				Files;					// if Files.FileList is NULL, then the main comment will be modified

			public uint TimeSet;		// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;			// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACESetComments() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACESetComments(string ArchiveName, ref sACESetCommentsStruc SetComments);


		//-------------------------  ACEEncryptFiles  -----------------------------
		// Encrypt specified files of archive using the given password.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACEEncryptFiles() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEEncryptFilesStruc
		{
			public sACEFilesStruc Files;		// files to encrypt/decrypt see tACEFilesStruc structure;

			public string EncryptPassword;      // if empty, the files will be decrypted (if they were encrypted) (maxlen=56)
			public string DecryptPassword;      // already encrypted files will have to be decrypted
												// before being encrypted with the new password
			
			public uint TimeSet;				// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;					// has to be filled with zeroes
		}

		
		//-----------------------------------------------------------------------
		// ACEEncryptFiles() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEEncryptFiles(string ArchiveName, ref sACEEncryptFilesStruc EncryptFiles);


		//---------------------------  ACEAddSFX  ---------------------------------
		// Add SFX module to archive.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACEAddSFX() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEAddSFXStruc
		{
			public string SFXName;				// set SFXName to ACE_SFX_DEFAULT if you want to add the default SFX module
			public string SFXArchiveName;		// returns the new name of the archive (should be *.exe)

			public uint TimeSet;				// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;					// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACEAddSFX() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEAddSFX(string ArchiveName, ref sACEAddSFXStruc AddSFXModule);


		//----------------------------  ACELock  ----------------------------------
		// Lock archiv, so that it can not be modified any more.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACELock() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACELockStruc
		{
			public uint TimeSet;		// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;			// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACELock() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACELock(string ArchiveName, ref sACELockStruc Lock);


		//----------------------  ACEAddRecoveryRecord  ---------------------------
		// Adds recovery record to archive.
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACEAddRecoveryRecord() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEAddRecoveryRecordStruc
		{
			public uint TimeSet;		// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;			// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACEAddRecoveryRecord() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEAddRecoveryRecord(string ArchiveName, ref sACEAddRecoveryRecordStruc AddRecoveryRecord);


		//----------------------------  ACEAddAV  ---------------------------------
		// Add AV to archive. (not supported in ACL)
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACEAddAV() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACEAddAVStruc
		{
			public uint TimeSet;		// see ACE_SETTIME constants

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;			// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACEAddAV() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEAddAV(string ArchiveName, ref sACEAddAVStruc AddAV);


		//--------------------------  ACERegister  --------------------------------
		// Registers AceV2.Dll. Program has to tell the 27digit-registration-code
		// first. If the key is not a general but a user-specific one, the name
		// of the user and his AV will be retrieved via RequestCallbackProc
		// (with ACE_CALLBACK_REQUEST_REGISTER). The key will be saved in the
		// keyfile specified in ACEGlobalDataStruc. (not supported in ACL)
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACERegister() parameter structure.
		//-----------------------------------------------------------------------

		[StructLayout(LayoutKind.Sequential, Pack=1)]
			public struct sACERegisterStruc
		{
			[MarshalAs(UnmanagedType.ByValArray, SizeConst=33)]
			public char[] RegCode;          // registration code in the format "ACEaaaabbbbccccdddd"

			[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
			byte[]  Reserved;				// has to be filled with zeroes
		}


		//-----------------------------------------------------------------------
		// ACERegister() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACERegister(ref sACERegisterStruc Register);


		//--------------------------  ACERegister  --------------------------------
		// Registers AceV2.Dll. Program has to tell the 27digit-registration-code
		// first. If the key is not a general but a user-specific one, the name
		// of the user and his AV will be retrieved via RequestCallbackProc
		// (with ACE_CALLBACK_REQUEST_REGISTER). The key will be saved in the
		// keyfile specified in ACEGlobalDataStruc. (not supported in ACL)
		//-------------------------------------------------------------------------

		//-----------------------------------------------------------------------
		// ACERegister() function declaration.
		//-----------------------------------------------------------------------

		[DllImport("AceV2.Dll")]
		public static extern int ACEShowRegisterDialog();

	}
}

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
Founder
Hong Kong Hong Kong

Comments and Discussions