Exemplo n.º 1
0
        internal ZipArchive(Stream stream, IPlatformOptions options, OpenFlags flags = OpenFlags.RDOnly)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            Options = options;
            Native.zip_error_t errorp;
            var    streamHandle = GCHandle.Alloc(stream, GCHandleType.Normal);
            IntPtr h            = GCHandle.ToIntPtr(streamHandle);
            IntPtr source       = Native.zip_source_function_create(callback, h, out errorp);

            archive = Native.zip_open_from_source(source, flags, out errorp);
            if (archive == IntPtr.Zero)
            {
                // error;
                string message = null;
                var    error   = (ErrorCode)errorp.zip_err;
                switch (error)
                {
                case ErrorCode.Exists:
                    message = $"The file already exists";
                    break;

                case ErrorCode.Incons:
                    message = $"The stream failed consistency checks";
                    break;

                case ErrorCode.Memory:
                    message = "libzip returned out of memory error";
                    break;

                case ErrorCode.NoEnt:
                    message = $"Stream does not exist and file creation wasn't requested";
                    break;

                case ErrorCode.NoZip:
                    message = $"Stream is not a ZIP archive";
                    break;

                case ErrorCode.Open:
                    message = $"Stream could not be opened";
                    break;

                case ErrorCode.Read:
                    message = $"Error occured while reading the Stream";
                    break;

                case ErrorCode.Seek:
                    message = $"Stream does not support seeking";
                    break;

                case ErrorCode.OK:
                    break;

                default:
                    message = $"Unexpected libzip error: {error}";
                    break;
                }

                if (!String.IsNullOrEmpty(message))
                {
                    throw new ZipIOException(message, error, Utilities.Errno);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Open ZIP archive at <paramref name="path"/> using <see cref="FileMode"/> specified in the
        /// <paramref name="mode"/> parameter. If <paramref name="strictConsistencyChecks"/> is <c>true</c>
        /// some extra checks will be performed on the ZIP being opened. If <paramref name="defaultExtractionDir"/> is
        /// not <c>null</c> or empty it is used by default by all the entries as the destination directory. Otherwise the
        /// current directory is used as the destination. Output directory can be different for each entry, see <see cref="ZipEntry.Extract"/>
        /// </summary>
        /// <param name="path">Path to the ZIP archive.</param>
        /// <param name="mode">File open mode.</param>
        /// <param name="defaultExtractionDir">default target directory</param>
        /// <param name="strictConsistencyChecks">Perform strict consistency checks.</param>
        /// <returns>Opened ZIP archive</returns>
        public static ZipArchive Open(string path, FileMode mode, string defaultExtractionDir = null, bool strictConsistencyChecks = false, IPlatformOptions options = null)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Must not be null or empty", nameof(path));
            }
            var zip = CreateArchiveInstance(defaultExtractionDir, options);

            OpenFlags flags = OpenFlags.None;

            switch (mode)
            {
            case FileMode.Append:
            case FileMode.Open:
                break;

            case FileMode.Create:
                flags = OpenFlags.Create;
                break;

            case FileMode.CreateNew:
                flags = OpenFlags.Create | OpenFlags.Excl;
                break;

            case FileMode.OpenOrCreate:
                flags = OpenFlags.Create;
                break;

            case FileMode.Truncate:
                flags = OpenFlags.Truncate;
                break;
            }

            if (strictConsistencyChecks)
            {
                flags |= OpenFlags.CheckCons;
            }

            ErrorCode error   = zip.Open(path, flags);
            string    message = null;

            switch (error)
            {
            case ErrorCode.Exists:
                message = $"The file {path} already exists";
                break;

            case ErrorCode.Incons:
                message = $"The file {path} failed consistency checks";
                break;

            case ErrorCode.Memory:
                message = "libzip returned out of memory error";
                break;

            case ErrorCode.NoEnt:
                message = $"File {path} does not exist and file creation wasn't requested";
                break;

            case ErrorCode.NoZip:
                message = $"File {path} is not a ZIP archive";
                break;

            case ErrorCode.Open:
                message = $"File {path} could not be opened";
                break;

            case ErrorCode.Read:
                message = $"Error occured while reading {path}";
                break;

            case ErrorCode.Seek:
                message = $"File {path} does not support seeking";
                break;

            case ErrorCode.OK:
                break;

            default:
                message = $"Unexpected libzip error: {error}";
                break;
            }

            if (!String.IsNullOrEmpty(message))
            {
                throw new ZipIOException(message, error, Utilities.Errno);
            }
            return(zip);
        }
Exemplo n.º 3
0
 public static void Init(SynchronizationContext uiContext, IPlatformOptions options)
 {
     ThreadHelper.Init(uiContext);
     PlatformOptions = options;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Create a new archive using the Stream provided. The steam should be an empty stream, any existing data will be overwritten.
 /// </summary>
 /// <param name="stream">The stream to create the arhive in</param>
 /// <param name="options">Platform-specific options</param>
 public static ZipArchive Create(Stream stream, IPlatformOptions options = null)
 {
     return(ZipArchive.CreateInstanceFromStream(stream, OpenFlags.Create | OpenFlags.Truncate, options));
 }
Exemplo n.º 5
0
 public static void Init(IPlatformOptions options)
 {
     Init(SynchronizationContext.Current, options);
 }