How File Works In Real Life.

How File Works In Real Life.:

By ridhaos Nov, 2024

What is a File Extension?

Have you ever wondered why every file has a specific extension, such as .txt, .jpg, or .pdf? Why do we need these extensions, and how do they work? Why does each file format require specific software to open it? Can you even create your own custom file extension?


In this article, we’ll answer these questions and explore how file extensions function. We’ll examine real examples and walk through creating a basic custom file extension from scratch to understand how it works.

Understanding File Extensions and Types

Before diving in, it’s essential to understand that, in computing, files generally fall into two main categories: 1. Text Files: Text files store data in a human-readable format using ASCII or Unicode characters. These files typically contain plain text, like words and symbols, that can be read and edited easily by humans. Common text file formats include .txt, .html, .xml, and .py (for Python scripts). 2. Binary Files: Binary files store data in a format that is not directly readable by humans; instead, they are read by machines. These files consist of a sequence of bytes and often represent complex data like images, audio, or executable code. Examples of binary file formats include .jpg (image), .mp3 (audio), and .exe (executable program).

The key difference between these types is readability: text files are readable by humans, while binary files are designed for machine interpretation.


Why Do File Extensions Matter?

File extensions serve as identifiers that tell the operating system and software applications what type of data a file contains and which program should be used to open it. For example: • A file with a .docx extension is typically associated with Microsoft Word. • A .jpg file is associated with image viewers or editors. • A .py file suggests that the file contains Python code, which can be run by a Python interpreter.

Extensions help the system and users quickly identify the file’s purpose and ensure that it opens with the correct software. Without these extensions, the computer would not automatically know how to handle different file types, leading to confusion and errors.


Can You Create Your Own File Extension?

Yes, you can create a file with any custom extension you like. Creating a custom file extension can be as simple as renaming a file with a unique suffix (e.g., .myext). However, for this file to be useful, you would typically need to develop or designate software that can open and interpret its contents.

In the next section, we’ll go through a simple example to create a custom file with a basic text-based extension and understand how it works.


Creating a Basic Custom Text File Extension

Let’s start by working with text files, as they are the simplest to understand. Many common file formats, such as .txt, .html, .xml, and .py (Python), are stored in plain text, making them human-readable and easy to create and edit.

Example: Creating a Simple Python File with a .py Extension

One popular text-based file format is the Python file, which has the .py extension. This extension indicates that the file contains Python code, which can be executed by the Python interpreter.

Let’s create a basic Python file, hello.py, with the following contents:

# hello.py
print("Hello, world!")

This file, with the .py extension, can be run by the Python interpreter. The .py extension tells the operating system that the file is a Python script, so double-clicking it (or running it in the terminal) will execute the code within the file.

You could experiment by creating a file with a different, custom extension—say, hello.myext. However, unless a specific program knows how to interpret .myext files, the file will likely not be usable beyond storing text.


Creating a Basic Custom Binary File Extension

Let’s explore creating a custom binary file. One of the most common binary file formats is .png. Let’s create a simplified example of how a .png file might be structured to understand its complexity.

Understanding the PNG File Structure

To create a PNG file from scratch and understand its structure, you’ll need to get familiar with the PNG format specifications. PNG (Portable Network Graphics) files are composed of a series of chunks, each with specific roles like metadata, image data, and color information. Here’s a basic breakdown:

PNG File Structure:

  1. PNG Signature (8 bytes): Every PNG file begins with the same 8-byte signature, which is:
89 50 4E 47 0D 0A 1A 0A

This sequence helps tools and software recognize that the file is a PNG image.

  1. Chunks: After the signature, PNG files are composed of a series of chunks. Each chunk has the following structure: • Length (4 bytes): The length of the chunk data. • Chunk Type (4 bytes): The type of the chunk (e.g., IHDR, IDAT, IEND). • Chunk Data (variable length): The actual data for this chunk. • CRC (4 bytes): A CRC-32 checksum of the chunk type and chunk data. There are critical chunks that must appear in every PNG file and ancillary chunks that provide additional information. Important PNG Chunks: • IHDR (Image Header Chunk): The first chunk that specifies image width, height, bit depth, color type, and more. • PLTE (Palette Chunk): Contains the color palette, if used. • IDAT (Image Data Chunk): Contains the actual image data, compressed using the DEFLATE algorithm. • IEND (Image End Chunk): Marks the end of the PNG file.

Basic Steps to Create a PNG from Scratch

  1. Write the PNG signature.
  2. Create an IHDR chunk: Contains the image width, height, and basic properties like color depth and compression method.
  3. Create an IDAT chunk: This chunk holds the compressed pixel data.
  4. Add an IEND chunk: Marks the end of the PNG file.

Let's putt all in practice by building program that create image with random color pixel:

#GeneratePNG.py
class GeneratePNG():
    # Define PNG Signature: 89 50 4E 47 0D 0A 1A 0A
    PNG_SIGNATURE = b'\x89PNG\r\n\x1a\n'

    def __init__(self, width:int = 8) -> None:
        # Here will create PNG file with defined width
        self.width = width;  
        self.size = width * width;   
        self.image_bytes = None
        self.raw_data = None
        self.png_data = None

        # Here we will pick random color from predefine list of COLOR
        rendImg = random.choice(list(Color)).value
        for a in range(0, self.size-1):
            rendImg += random.choice(list(Color)).value

        # Compress Color code into struct with len - prepare IDAT 
        self.image_bytes = struct.pack(">%uB" % len(rendImg), * rendImg)
        self.raw_data = b''.join([b'\x00' + self.image_bytes[i:i + width * 4] for i in range(0, len(self.image_bytes), width*4)])

    # This method to create chunks for IHDR, IDAT, IEND,... by adding length and CRC.
    @staticmethod
    def create_chunk(chunk_type, data):
        length = struct.pack(">I", len(data))
        chunk_type = chunk_type.encode('ascii')
        crc = struct.pack(">I", zlib.crc32(chunk_type+data) & 0xFFFFFFFF)
        return length + chunk_type + data + crc

    #This method to generate png file.
    def generate_png(self):
        # Use Deflate Algorithm to compress data.
        compressed_data = zlib.compress(self.raw_data)
        ihdr_data = struct.pack(">IIBBBBB", self.width, self.width, 8, 6, 0, 0, 0)
        ihdr_chunk = Cube.create_chunk("IHDR", ihdr_data)
        idat_chunk = Cube.create_chunk("IDAT", compressed_data)
        iend_chunk = Cube.create_chunk("IEND", b'')
        # Parse all chunks together.
        self.png_data = Cube.PNG_SIGNATURE  + ihdr_chunk + idat_chunk +iend_chunk

    # Save all to bin file with png extension
    def save_png(self, output:str = './output'):
        output += ".png"
        with open(output, 'wb') as f:
            f.write(self.png_data)

How Text and Binary Files Are Handled

As discussed earlier, the way data is stored in text files differs from binary files. Text files store readable text, while binary files store data in a format that is only machine-readable.

For instance:

Text File (e.g., .txt, .html): Data in text files is stored in a form that can be displayed and edited in any text editor. • Binary File (e.g., .jpg, .mp3): Data in binary files is encoded in a way that requires specific software to decode and display the information.


Summary:

Key Takeaways on File Extensions

  1. File extensions serve as a shorthand to identify the file type and associate it with appropriate software.
  2. Text files are human-readable and can be opened with basic text editors, while binary files require specific programs to interpret their data.
  3. Custom file extensions are possible but may need custom software to be functional or meaningful.

With this understanding, you now know how file extensions work, why they are essential, and how you can experiment with creating your own file extensions. Exploring custom file types and understanding the basics of file handling can deepen your knowledge of computer systems and programming.