我将如何创建自己的 zip 文件? [英] How would I create my own zip like file?

查看:45
本文介绍了我将如何创建自己的 zip 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个类似于 zip 文件但同时又不是实际 zi​​p 文件的文件.

I want to be able to create a file that'll act like a zip but at the same time it isn't an actual zip.

假设我有一个程序,它将接收一堆文件和目录并将它们存储到一个名称和扩展名为 data.rds 的文件中,并且您需要相同的程序来从中提取它们.我在许多不同的游戏中看到他们使用文件格式,例如 .arc.nsa.mxdl 等,它们都存储了许多其中的文件,.rar 可能是最常见的格式.这四个扩展无法作为普通 zip 打开,需要特定程序才能从中提取文件,我想了解如何将多个文件加密和解密为一个文件,而不使其具有可读性将在一个普通的 zip 文件中.

Let's say I have a program that'll take a bunch of files and directories and store them into a single file with a name and extention of data.rds and you would need the same program to extract them out of it. I've seen in lots of different games that they use file formats such as .arc, .nsa, .mxdl etc which all store many files inside of them, .rar is probably the most commonly known format. The four extentions can't be opened as a normal zip and require a specific program in order to extract the files from them, I want to learn as to how you would encrypt and decrypt many files into a single one without making it readable like it would be in a normal zip file.

几乎如何去做这件事?我知道这将是一个漫长的过程,不会用几行简单的代码来回答,但如果有人能指出我学习如何做这样的事情的方向,那会很有帮助.

Pretty much how would one go about doing this? I know it would be a long process and won't be answered with a few simple lines of code but if someone could point me in a direction towards learning as to how to do such a thing, that would help helpful.

推荐答案

无论您发明什么格式,都会有人解决.任何人都可以反编译您的代码并查看您的算法.

No matter what format you invent, someone will figure it out. Anyone can decompile your code and see your algorithm.

我只想使用 Zip 格式并为文件指定不同的扩展名(听起来您已经这样做了).防止随意观察者打开文件的一种简单方法是在文件前面放几个垃圾字节:

I would just use the Zip format and give the file a different extension (which it sounds like you're already doing). An easy way to keep casual observers from opening your file is to put a couple junk bytes at the front of it:

private static final byte[] secretSignature = { 10, 20 };

void writeData(Path file)
throws IOException {
    try (OutputStream out = new BufferedOutputStream(
            Files.newOutputStream(file))) {
        out.write(secretSignature);

        ZipOutputStream zip = new ZipOutputStream(out);
        // Write zip entries
        zip.finish();
    }
}

void readData(Path file)
throws IOException {
    try (InputStream in = new BufferedInputStream(
            Files.newInputStream(file))) {
        in.skip(secretSignature.length);

        ZipInputStream zip = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zip.getNextEntry()) != null) {
            // Read entry
        }
    }
}

这篇关于我将如何创建自己的 zip 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆