Ejemplo n.º 1
0
        public static void View(string zipFileName)
        {
            ZipReader reader = new ZipReader(zipFileName);

            Console.WriteLine("Archive: {0} ({1} files)", zipFileName, reader.Entries.Count);
            Console.WriteLine(reader.Comment);

            string format = "{0,8} {1,8} {2,5} {3,10} {4,5} {5}";
            Console.WriteLine(format, " Length ", "  Size  ", "Ratio", "   Date   ", "Time ", "Name");
            Console.WriteLine(format, "--------", "--------", "-----", "----------", "-----", "----");

            foreach (ZipEntry entry in reader.Entries) {
                if (!entry.IsDirectory) {
                    Console.WriteLine(format,
                        entry.Length,
                        entry.CompressedLength,
                        entry.Ratio.ToString("P0"),
                        entry.ModifiedTime.ToString("yyyy-MM-dd"),
                        entry.ModifiedTime.ToString("hh:mm"),
                        entry.Name);
                }
            }
            reader.Close();
        }
Ejemplo n.º 2
0
        public static void Extract(string zipFileName)
        {
            ZipReader reader = new ZipReader(zipFileName);
            Console.WriteLine("Archive: {0}", zipFileName);
            Console.WriteLine(reader.Comment);

            // buffer to hold temp bytes
            byte[] buffer = new byte[4096];
            int byteCount;

            // Get the zipped entries
            while (reader.MoveNext()) {
                ZipEntry entry = reader.Current;

                if (entry.IsDirectory) {
                    Directory.CreateDirectory(entry.Name);
                } else {
                    Console.Write("  {0}", entry.Name);

                    // create output stream
                    FileStream writer = File.Open(entry.Name, FileMode.Create);

                    // write uncompressed data
                    while ((byteCount = reader.Read(buffer, 0, buffer.Length)) > 0) {
                        Console.Write(".");
                        writer.Write(buffer, 0, byteCount);
                    }
                    writer.Close();
                    Console.WriteLine();
                }
            }
            reader.Close();
        }
Ejemplo n.º 3
0
        protected int UnZip(string ZipFileName, bool CreateDir)
        {
            ZipReader reader = null;
            int i = 0;

            //'Figure out new directory name for the uncompressed files
            string NewPath = "";
            NewPath = ZipFileName.Replace(Path.GetExtension(ZipFileName), "");

            //'Make a directory with the name of the zip file. Overwritten if exists.
            if (CreateDir == true)
            {
                Directory.CreateDirectory(NewPath);
            }
            else
            {
                NewPath = Path.GetDirectoryName(NewPath);
            }

            try
            {
                //'Open a zip file
                reader = new ZipReader(ZipFileName);
                //'Process all files inside the archive
                while (reader.MoveNext())
                {
                    i += 1;
                    //'Create a Zip object
                    ZipEntry entry = reader.Current;

                    if (!entry.IsDirectory)
                    {
                        //'It's a file
                        string FileName = entry.Name.Replace("/", "\\");
                        //'If entry is Directory, create it
                        if (FileName.IndexOf("\\") > 0)
                        {
                            //'If Directory exists, it is not overwritten
                            try
                            {
                                Directory.CreateDirectory(NewPath + "\\" + Path.GetDirectoryName(FileName));
                            }
                            catch (UnauthorizedAccessException ex)
                            {
                                ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_CREATE_DIRECTORY_ACCESSDENIED"), MessageType.ErrorMsg);
                                return 0;
                            }
                            catch (Exception ex)
                            {
                                ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_CREATE_DIRECTORY") + " " + ex.Message, MessageType.ErrorMsg);
                                return 0;
                            }
                        }

                        //'Uncompress the file if its type is not forbidden
                        //'for upload
                        if (IsAllowedFileType(Path.GetExtension(FileName)))
                        {
                            FileStream Writer = null;
                            try
                            {
                                Writer = File.Open(NewPath + "\\" + FileName, FileMode.Create);

                                Byte[] buffer = new Byte[4096];
                                int byteCount = 0;

                                //'Read file from the archive and write it to the disk
                                byteCount = reader.Read(buffer, 0, buffer.Length);
                                while (byteCount > 0)
                                {
                                    Writer.Write(buffer, 0, byteCount);
                                    byteCount = reader.Read(buffer, 0, buffer.Length);
                                }
                            }
                            finally
                            {
                                //'Close the file
                                Writer.Close();
                            }
                        }
                    }
                }
            }

            catch (UnauthorizedAccessException ex)
            {
                ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_ZIP_CANNOTEXTRACT_ACCESSDENIED"), MessageType.ErrorMsg);
                return i;
            }
            catch (Exception ex)
            {
                ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_COPY_CANNOTEXTRACT") + " " + ex.Message, MessageType.ErrorMsg);
                return i;
            }
            finally
            {
                //'Close the Zip file
                reader.Close();
            }

            //'Return the number of files produced
            return i;
        }