Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

analyzing zipped or any archive file

I was wondering if anyone can recommend a tool to analyze zipped or any archive file. I do not mean checking what is inside the archive but more about how it was compressed, with what compression method, etc.

Thanks!

like image 511
w1ck3d64 Avatar asked Oct 23 '25 18:10

w1ck3d64


2 Answers

For data compressed into a ZIP file, the command-line tool zipinfo is quite helpful, particularly when using the '-v' argument (for verbose mode). I learned of zipinfo from this zip-related question on SuperUser

like image 91
pestophagous Avatar answered Oct 26 '25 19:10

pestophagous


I recently ran into an issue where the zip's being created by one tool would only open with certain programs and not others. The issue turned out to be that directories didn't have entries in the zip file, they were just implied by the presence of files in them. Also all the directory separators were backslashes instead of forward slashes.

zipinfo didn't really help with these bits. I needed to see the zip entries so I ended up writing this the following which allowed me diff the directory entries with a known good version

using System;
using System.IO;
using System.Text;

namespace ZipAnalysis
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("No filename specified");
                Console.WriteLine("Press any key to exit");
                Console.ReadKey(true);
                return;
            }
            string fileName = args[0];
            if (!File.Exists(fileName))
            {
                Console.WriteLine($"File not found: {fileName}");
                Console.WriteLine("Press any key to exit");
                Console.ReadKey(true);
                return;
            }

            using (var file = File.OpenRead(fileName))
            {
                //First, find the End of central directory record

                BinaryReader reader = new BinaryReader(file);
                int entryCount = ReadEndOfCentralDirectory(reader);
                if (entryCount > 0)
                {
                    ReadCentralDirectory(reader, entryCount);
                }
            }
            Console.WriteLine("Press any key to exit");
            Console.ReadKey(true);
        }

        private static int ReadEndOfCentralDirectory(BinaryReader reader)
        {
            var b = reader.ReadByte();
            int result = 0;
            long fileSize = reader.BaseStream.Length;
            while (result == 0 && reader.BaseStream.Position < fileSize)
            {
                while (b != 0x50)
                {
                    if (reader.BaseStream.Position < fileSize)
                        b = reader.ReadByte();
                    else
                        break;
                }

                if (reader.BaseStream.Position >= fileSize)
                {
                    break;
                }

                if (reader.ReadByte() == 0x4b && reader.ReadByte() == 0x05 && reader.ReadByte() == 0x06)
                {
                    int diskNumber = reader.ReadInt16();
                    int centralDirectoryStartDiskNumber = reader.ReadInt16();
                    int centralDirectoryCount = reader.ReadInt16();
                    int centralDirectoryTotal = reader.ReadInt16();
                    result = centralDirectoryTotal;
                    int centralDirectorySize = reader.ReadInt32();
                    int centralDirectoryOffset = reader.ReadInt32();
                    int commentLength = reader.ReadInt16();
                    string comment = Encoding.ASCII.GetString(reader.ReadBytes(commentLength));
                    Console.WriteLine("EOCD Found");
                    Console.WriteLine($"Disk Number: {diskNumber}");
                    Console.WriteLine($"Central Directory Disk Number: {centralDirectoryStartDiskNumber}");
                    Console.WriteLine($"Central Directory Count: {centralDirectoryCount}");
                    Console.WriteLine($"Central Directory Total: {centralDirectoryTotal}");
                    Console.WriteLine($"Central Directory Size: {centralDirectorySize}");
                    Console.WriteLine($"Central Directory Offset: {centralDirectoryOffset}");
                    Console.WriteLine($"Comment: {comment}");
                    reader.BaseStream.Seek(centralDirectoryOffset, SeekOrigin.Begin);
                }
            b=0;
            }
            return result;
        }

        private static void ReadCentralDirectory(BinaryReader reader, int count)
        {
            for (int i = 0; i < count; i++)
            {
                var signature = reader.ReadInt32();
                if (signature == 0x02014b50)
                {
                    Console.WriteLine($"Version Made By: {reader.ReadInt16()}");
                    Console.WriteLine($"Minimum version to extract: {reader.ReadInt16()}");
                    Console.WriteLine($"Bit Flag: {reader.ReadInt16()}");
                    Console.WriteLine($"Compression Method: {reader.ReadInt16()}");
                    Console.WriteLine($"File Last Modification Time: {reader.ReadInt16()}");
                    Console.WriteLine($"File Last Modification Date: {reader.ReadInt16()}");
                    Console.WriteLine($"CRC: {reader.ReadInt32()}");
                    Console.WriteLine($"CompressedSize: {reader.ReadInt32()}");
                    Console.WriteLine($"UncompressedSize: {reader.ReadInt32()}");
                    var fileNameLength = reader.ReadInt16();
                    var extraFieldLength = reader.ReadInt16();
                    var fileCommentLength = reader.ReadInt16();
                    Console.WriteLine($"Disk number where file starts: {reader.ReadInt16()}");
                    Console.WriteLine($"Internal file attributes: {reader.ReadInt16()}");
                    Console.WriteLine($"External file attributes: {reader.ReadInt32()}");
                    Console.WriteLine($"Relative offset of local file header: {reader.ReadInt32()}");
                    string filename = Encoding.ASCII.GetString(reader.ReadBytes(fileNameLength));
                    string extraField = Encoding.ASCII.GetString(reader.ReadBytes(extraFieldLength));
                    string fileComment = Encoding.ASCII.GetString(reader.ReadBytes(fileCommentLength));
                    Console.WriteLine($"Filename: {filename}");
                    Console.WriteLine($"Extra Field: {extraField}");
                    Console.WriteLine($"File Comment: {fileComment}");
                }
            }
        }
    }
}
like image 44
James Barrass Avatar answered Oct 26 '25 19:10

James Barrass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!