How to recover deleted files using Java? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to recover files from a disk by Java without using native libraries
I'm doing this using Java 8
As far as I know deleted files remain on the disk until they are overwritten
I have direct access to disk on linux and I can read raw data, but, how can I parse deleted files on an ext4 or NTFS file system for example?
Thanks.

Recovering deleted files requires knowledge of how the underlaying file system is implemented, so you have a bit of reading to do before you can get anywhere.
In theory, YES, you can definitely do this in pure Java; you just need to find out how to read data from a raw disk, bypassing the file system. On a Unix system this is simple: open the device node as a file (you'll need root permissions) and just read. On Windows there is probably a similar process; at worst you'll have to create a helper library in C or C++ to read the data for you.
Once you get access to the raw data, look up how files are stored in your particular file system and start looking for similar patterns in the data that you read.
This is not something you can do in an afternoon though.
Update: How to bypass the file system.
On a Unix system you can read from a partition or volume like this:
InputStream sda1 = new FileInputStream("/dev/sda1");
int firstByte = sda1.read();
On Windows you would read from \\.\PhysicalDisk0. From Naming Files, Paths, and Namespaces:
Another example of using the Win32 device namespace is using the CreateFile function with "\\.\PhysicalDiskX" (where X is a valid integer value) or "\\.\CdRomX". This allows you to access those devices directly, bypassing the file system. This works because these device names are created by the system as these devices are enumerated, and some drivers will also create other aliases in the system. For example, the device driver that implements the name "C:\" has its own namespace that also happens to be the file system.
APIs that go through the CreateFile function generally work with the "\\.\" prefix because CreateFile is the function used to open both files and devices, depending on the parameters you use.
If you're working with Windows API functions, you should use the "\\.\" prefix to access devices only and not files.
Most APIs won't support "\\.\"; only those that are designed to work with the device namespace will recognize it. Always check the reference topic for each API to be sure.
I don't know if the Java API is implemented using CreateFile or if it does some name mangling that means you can't access the device namespace. In the worst case you'll have to create a wrapper library that calls CreateFile and turns the HANDLE it returns into a file descriptor that can be used in Java; that's no work at all.

Files by definition are named sequences of bytes stored on permanent storage device.
Files are managed by OS component named file system. File system operates with term "file" and translates this term to lower level terms like volume, sector, block etc.
Mapping between file name (and path) and blocks on your disk where the information is actually stored is named files table and is managed by file system.
When you delete file you ask file system to remove appropriate entry from file table. This means that indeed the file content is not deleted from disk physically and if you are lucky enough can probably be restored. Why probably? Because once the file entry is removed from the table the space occupied by file can be re-used and therefore other information can be stored there.
There are tools that try to restore the information. These tools work on level under file system, i.e. use lower level APIs. Probably they are talking directly to driver. Java does not provide API for doing this.
Therefore you have the following solutions.
Implement this task in native language.
Use existing tools that do this task and provide either API or command line interface.

Related

Java encryption on-the-fly by virtual drive

I have made an encryption system and I'm looking for a way to add some way to integrate it with file system on windows(if possible also on linux). I don't want to rise a debate whether it is needed, that it already exists etc...
I was hoping to find a way to mount a virtual disk drive that will be able to access the files in the decrypted form, encrypt and decrypt on the fly using my software, it is currently written in java, but if needed I can port it to c++.
I have found one way to do it, which is to run a java ssh server and use another software to mount it in windows, but it doesn't work work well, constant crashes or it sometimes just doesn't mount the drive.
I need it as I want to access the files using IDE and other programs without coping the files as it decreases the security and doubles the disk space used.
Has anyone found a way to do it preferably in java?
Is there some kind of API for it (all I need is list files, get parent, read file, write file)?
Or is there a good java lib what works with another program to do it?

Audit access to files in file systems

Is there a way to know if a file in the file system was read/put in the clipboard?
I'm using java and I'm thinking about this approach for a question I've posted previously.
Lock a file for reading even from Operating System except a single process
I don't have a specific technology, but I would like to solve this problematic with Java.

SoundPool: What is the "path" in the "load(String path, int priority)"

Here's a question I had stumbled upon a few days ago.
The question is asking for how the method, "load(String path, int priority)" should be used, and what syntax the "path" variable should be like. Unfortunately, all the answers to that question gives incorrect answers. None of them mentions the use of the "load(String, int)" method, and none of them even answers what "path" should look like.
I'm unable to come up with a solution to this. Could anyone help out?
The path is just the path of the file in the android filesystem.
For example, If I drag/drop foo.mp3 to my android filesystem, it gets placed at /mnt/sdcard/foo.mp3 I would pass /mnt/sdcard/foo.mp3 to the load method.
The base path (/mnt/sdcard/ ) may vary from phone to phone, so you can query it with Environment.getExternalStorageDirectory().
As far as I know it's not to be used with resources, but rather just files in your filesystem.
Also, should probably include this note from Android docs, so that the "sdcard" is not confusing. My phone does not even have an external sdcard, but all the files in my android filesystem get placed in the /sdcard/ folder, I have no idea why.
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

creating own file extension [duplicate]

This question already has answers here:
How to create my own file extension like .odt or .doc? [closed]
(3 answers)
Closed 8 years ago.
I'm on my way in developing a desktop application using netbeans(Java Dextop Application) and I need to implement my own file format which is specific to that application only. I'm quite uncertain as to how should I go about first.What code should I use so that my java application read that file and open it in a way as I want it to be.
If it's character data, use Reader/Writer. If it's binary data, use InputStream/OutputStream. That's it. They are available in several flavors, like BufferdReader which eases reading a text file line by line and so on.
They're part of the Java IO API. Start learning it here: Java IO tutorial.
By the way, Java at its own really doesn't care about the file extension or format. It's the code logic which you need to write to handle each character or byte of the file according to some file format specification (which you in turn have to writeup first if you'd like to invent one yourself).
I am not sure this directly addresses your question, but since you mentioned a custom file format, it is worth noting that applications launched using Java Web Start can declare a file association. If the user double clicks one of those file types, the file name will be passed to the main(String[]) of the app.
This ability is used in the File Service demo. of the JNLP API - available at my site.
As to the exact format of the file & the best ways to load and save it, there are a large number of possibilities that can be narrowed down with more details of the information it contains.
Choosing a new/existing file extension does not affect your application (or in any case anyone's). It is upto the programmer what files he wants his app to read.
For example, you may consider you can't read a pdf or doc directly as a text file....but that is not because they are written/ stored differently, but because they have headers or characters which your app does not understand. So we might use a plugin or extension which understands those added headers ( or rather the grammar of the pdf /doc file) removes them & lets our app know what text (or anything else) it contains.
So if you wish to incorporate your own extension, & specifically want no other application to be able to read it, just write the text in a way that only your program is able to understand. Though writing a file in binary pretty much ensures that your file is not read directly just by user opening a file, but it is however still possible to read from it, if it is merely collection of raw characters.
If you ask code for hiding a data, I'd say there are plenty of algorithms you might use, which usually get tagged as encryptions cause you are basically trying to lock/hide your stuff. So if you do not really care for the big hulla-bulla, simply trying to keep a file from being directly read & successful attempts to read the file does not cause any harm to your application, write it in binary.

Create AppleDouble formatted file in Linux

I'm working on an application that syncs data. For Mac OS, files are uploaded and if they contain resource fork information, the fork is read and stored as a string using: file/..namedfork/rsrc
Users can access their files using a Web application(Java) that's running on a Linux server, is there a way that I can generate a valid AppleDouble format file using only the data fork and the string I read from the namedfork? I don't mind losing the Finder Metadata.
Note: The generated file will be downloaded (using the Web Application) as a single file for Mac OS users.
Is this possible?
Regards
As far as I'm aware, OS 9/OS X can only natively access the resource forks on files served by AppleTalk shares. For other media, e.g. SMB (Microsoft Networking) or HTTP, the only way to preserve the resource fork is to place the file in an archive.
There are several Mac-specific archive formats that support this, for example, StuffIt and HQX. I very much doubt the Linux binaries for StuffIt would allow packaging a resource fork from a separate file, but at least there is something for you to evaluate.
Looking at the AppleDouble Wikipedia entry, it seems it may be possible to create such a file from a non-Apple machine using an open source tool, and sending the resultant file using the multipart/appledouble MIME type. Perhaps you could call this binary from your Java code?
The wikipedia article states:
AppleSingle combined both file forks and the related Finder meta-file information into a single file, whereas AppleDouble stored them as two separate files.
The apple knowledgebase article states:
The second new file has the name of the original file prefixed by a "._ " and contains the resource fork of the original file.
So I assume you just have to save the content of your resource fork string into the appropriately named file.
Edit:
After your comment I'm not sure what you want. Your question was how to
Create AppleDouble formatted file in Linux
and the documentation I linked to shows that you need to create two files to do that one containing the data and one containing the resource fork with a name that has ._ prefixed. If that is not what you want then you need to ask a different question.

Categories