Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lib/archive] Support for handling compressed files #146

Open
gnh1201 opened this issue Sep 28, 2024 · 9 comments
Open

[lib/archive] Support for handling compressed files #146

gnh1201 opened this issue Sep 28, 2024 · 9 comments
Labels
commercial For customers with a willingness to pay enhancement New feature or request

Comments

@gnh1201
Copy link
Owner

gnh1201 commented Sep 28, 2024

Summary

We plan to provide support for handling compressed files in the scripting environment. We aim to use the built-in features of the operating system as much as possible and will also support some well-known commercial tools.

In addition to the operating system's built-in APIs for compressed files, we are also looking into open-source projects such as PeaZip and 7-Zip. Commercial software like WinRAR and WinZIP are also being considered.

Related links

@gnh1201 gnh1201 added enhancement New feature or request research Nobody spends money, but it is necessary labels Sep 28, 2024
@gnh1201
Copy link
Owner Author

gnh1201 commented Nov 7, 2024

I’ll let you know as soon as the initial version of lib/archive.js is ready. @pranulkbv28

@pranulkbv28
Copy link

pranulkbv28 commented Nov 7, 2024 via email

@gnh1201
Copy link
Owner Author

gnh1201 commented Nov 7, 2024

I updated the package.json to resolve the issue where 'npm run start' was not working during testing. The project directory structure changed, and this update was missed. Please update the project to the latest version and try again. Thank you.

@gnh1201 gnh1201 added commercial For customers with a willingness to pay priority:high Be crazy. Do fast. collaboration Related to a collaboration and removed research Nobody spends money, but it is necessary labels Nov 7, 2024
@gnh1201
Copy link
Owner Author

gnh1201 commented Nov 13, 2024

If you refer to this code, it will help you write it.

// archive.js

var ArchiveObject = function() {
    this.engine = "HOW_TO_COMPRESS_AND_DECOMPRES_A_FILE";
  
    this.create = function(engine) {
        this.engine = engine;
    }

    this.extractTo = function(targetDirectory) {
        if (this.engine == "METHOD_1") {
            // how to extract a file with METHOD_1
        } else (this.engine == "METHOD_2") {
            // how to extract a file with METHOD_2
        } // ...
    }

    this.compressDirectory = function(sourceDirectory) {
        if (this.engine == "METHOD_1") {
            // how to compress a file with METHOD_1
        } else (this.engine == "METHOD_2") {
            // how to compress a file with METHOD_2
        } // ...
    }

    // A good practice is lib/http.js
}

@pranulkbv28
Copy link

this is what I have come up with
Let us discuss on this tmrw

// archive.js

const SHELL = require("lib/shell"); // Assuming a shell library for executing external tools
const OS = require("lib/system");  // For OS-specific commands

var ArchiveObject = function(engine) {
    this.engine = engine || "BUILT_IN"; // Default to built-in OS tools
    this._interface = null;

    // Initialize the archive engine
    this.create = function() {
        switch (this.engine) {
            case "BUILT_IN":
                this._interface = new BuiltInEngine();
                break;
            case "7ZIP":
                this._interface = new SevenZipEngine();
                break;
            case "WINRAR":
                this._interface = new WinRarEngine();
                break;
            default:
                throw new Error("Unsupported engine: " + this.engine);
        }
    };

    // Compress a directory
    this.compress = function(sourceDirectory, outputFile) {
        if (!this._interface) this.create();
        return this._interface.compress(sourceDirectory, outputFile);
    };

    // Decompress an archive
    this.extract = function(archiveFile, targetDirectory) {
        if (!this._interface) this.create();
        return this._interface.extract(archiveFile, targetDirectory);
    };
};

// Base interface for engines
class BaseEngine {
    compress(sourceDirectory, outputFile) {
        throw new Error("compress() not implemented.");
    }

    extract(archiveFile, targetDirectory) {
        throw new Error("extract() not implemented.");
    }
}

// Built-in OS tools
class BuiltInEngine extends BaseEngine {
    compress(sourceDirectory, outputFile) {
        const osName = OS.getOS();
        if (osName === "Windows") {
            // Example for Windows using PowerShell
            return SHELL.exec(`Compress-Archive -Path "${sourceDirectory}\\*" -DestinationPath "${outputFile}"`);
        } else if (osName === "Linux" || osName === "Darwin") {
            // Example for Linux/Mac using tar
            return SHELL.exec(`tar -czf "${outputFile}" -C "${sourceDirectory}" .`);
        }
        throw new Error("Unsupported OS for BuiltInEngine.");
    }

    extract(archiveFile, targetDirectory) {
        const osName = OS.getOS();
        if (osName === "Windows") {
            // Example for Windows using PowerShell
            return SHELL.exec(`Expand-Archive -Path "${archiveFile}" -DestinationPath "${targetDirectory}"`);
        } else if (osName === "Linux" || osName === "Darwin") {
            // Example for Linux/Mac using tar
            return SHELL.exec(`tar -xzf "${archiveFile}" -C "${targetDirectory}"`);
        }
        throw new Error("Unsupported OS for BuiltInEngine.");
    }
}

// 7-Zip engine
class SevenZipEngine extends BaseEngine {
    compress(sourceDirectory, outputFile) {
        const command = `7z a "${outputFile}" "${sourceDirectory}\\*"`;
        return SHELL.exec(command);
    }

    extract(archiveFile, targetDirectory) {
        const command = `7z x "${archiveFile}" -o"${targetDirectory}"`;
        return SHELL.exec(command);
    }
}

// WinRAR engine
class WinRarEngine extends BaseEngine {
    compress(sourceDirectory, outputFile) {
        const command = `WinRAR a "${outputFile}" "${sourceDirectory}\\*"`;
        return SHELL.exec(command);
    }

    extract(archiveFile, targetDirectory) {
        const command = `WinRAR x "${archiveFile}" "${targetDirectory}"`;
        return SHELL.exec(command);
    }
}

// Factory function for creating an ArchiveObject
function createArchiveObject(engine) {
    const archive = new ArchiveObject(engine);
    archive.create();
    return archive;
}

// Example Usage
const archive = createArchiveObject("7ZIP");
archive.compress("C:\\MyFolder", "C:\\MyArchive.zip");
archive.extract("C:\\MyArchive.zip", "C:\\ExtractedFolder");

@gnh1201
Copy link
Owner Author

gnh1201 commented Nov 25, 2024

I’ve reviewed your code and am glad to see the effort you’ve put into studying.

However, if you plan to use inheritance, please note that the engine we use restricts the use of the class keyword, so you’ll need to adopt an alternative approach.

Instead of using class {a} extends {b}, you’ll need to implement class inheritance through prototype.

Here’s an example:

var STD = require("lib/std");

var SomethingEventableObject = function() {
    this.hello = "world";
};
SomethingEventableObject.prototype = new STD.EventTarget(); 
SomethingEventableObject.prototype.constructor = SomethingEventableObject;

For a practical example of this being used, you can refer to lib/chrome.js.

Thank you.

@gnh1201 gnh1201 pinned this issue Nov 25, 2024
@gnh1201 gnh1201 unpinned this issue Nov 26, 2024
@gnh1201
Copy link
Owner Author

gnh1201 commented Nov 27, 2024

A demonstration on file compression and decompression is expected to be scheduled soon, likely on December 3. Please prepare thoroughly for it. Thank you!

@pranulkbv28
Copy link

pranulkbv28 commented Nov 27, 2024 via email

@gnh1201
Copy link
Owner Author

gnh1201 commented Dec 2, 2024

We recently decided to collaborate with a new developer through a local venture support program. It has been a month since we began the collaboration and assigned issues, but we have not received any code updates or status updates from them. As a result, we have reluctantly decided to discontinue their involvement.

@gnh1201 gnh1201 removed priority:high Be crazy. Do fast. collaboration Related to a collaboration labels Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commercial For customers with a willingness to pay enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants