I am interested in creating a tool that can be run from the command line (pref. linux) and which will scour a select group of files (think .gitignore in reverse), parse those files, and create an image that looks like this Conceptboard. It doesn't need to look exactly like that, but I should be able to control graphical effects and layout of the boxed elements. Each boxed element should simply have the file name up top, and the contents of the file below.
My questions are:
What tools are available for this type of project?
Where would I logically start?
How long should I expect this to take me?
edit: graphviz could definitely work, but is it able to show file contents?
Graphviz can't directly show file contents, but I've often coupled it with code in java or python to build the appropriate .dot file, then run graphviz on that .dot file to produce the final graphic, something like this pseudocode:
open .dot text file and add graphviz header information
for each file in directory:
save filename for future reference
read and save file contents
create node in .dot file formatted with filename at top and contents below
for each file in directory:
parse file to locate links to other files
if filename has been processed above, add a link line to .dot output file
add graphviz footer to .dot file
run DOT on .dot file
the final .dot file would look something like this, only much longer:
digraph fileStructure {
node [shape=box, color=black, fontsize=14, fillcolor=white, style=filled]
1 [label="filename\nfile contents"]
2 [label="filename\nfile contents"]
3 [label="filename\nfile contents"]
1 -> 2
1 -> 3
}
There are libraries for most major programming language to simplify the process of creating .dot files and running graphviz, but it's not too hard to do directly.
How long it would take depends on your skill level, but I wouldn't think this would take more than a few hours to complete.
Related
I have a bunch of .txd and .dff files that have different names, let's say, like, img1 img30 greenimg3, etc. all in the same folder. I can compile a list of these names that I want to replace (but not all the files in the folder will be replaced), but how can I replace my list of files with just one file without altering the name of the file? So it remains img1, img30, greenimg3, etcetera but with the replaced file. I hope this makes sense. I would be grateful if someone could write something that I could use, as I don't know how to code myself.
not sure if i understand corretly, but you could do this in python:
import os
folder_path = "./folder"
with open("source.file", "rb") as source_file:
source_file_content = source_file.read()
for file in os.listdir(folder_path):
with open(os.path.join(folder_path, file), "wb") as destination_file:
destination_file.write(source_file_content)
where "./folder" is the path to your folder containing all files you want to overwrite and "source.file" is the path to your file you want to overwrite with.
this script reads and writes the files as binary
I need to store data in a file in this format
word, audio, jpeg
How would I store that all in one file? Is it even possible do would I need to store links to other data files in place of the audio and jpeg. Would I need a custom file format?
1. Your own filetype
As mentioned by #Ken White you would need to be creating your own custom file format for this sort of thing, which would then mean creating your own parser type. This could be achieved in almost any language you wanted but since you are planning on using word format, then maybe C# would be best for you. However, this technique could be quite complicated and take a relatively large amount of time to thoroughly test your file compresser / decompressor, but may be best depending on your needs.
2. Command line utilities
Another way to go about this would be to use a bash script to combine all of the files into one file, and then decompress it at the other end. For example the steps could involve:
Combine files using windows copy / linux cat command on command line
Create a metdata file of your own that says how many files are in this custom file, and how much memory each one takes up (could be a short XML or JSON file for example...)
Use the linux split command or install a Windows command line file splitter program (here's just one example) to split the file back into whatever components have made it up.
This way you only have to create a really small file type, and let the OS utilities handle the combining of them for you.
Example on Windows:
Copy all of the files in your current directory into one output file called 'file.custom'
copy /b * file.custom
Generate custom file format describing metadata (i.e. get the file size on disk in C# example here). This is just maybe what I would do in JSON. SO formatting was being annoying so here's a link (Copy paste it into an editor or online JSON viewer).
Use a decompress windows / linux command line tool to decompress each files to the exact length (and export it back to the exact name) specified in the JSON (metadata) file. (More info on splitting files on this post).
3. ZIP files
You could always store all of the files in a compressed zip file, and then just use a zip compressor, expander as and when you like to retreive any number of file formats stored within.
I found a couple of examples of :
Combining multiple files into one ZIP file in only C# .net,
Unzipping ZIP files in C#
Zipping & Unzipping with only windows built-in utilities
Zipping & Unzipping in Linux command line
Good Zipping/Unzipping library in Java
Zipping/Unzipping in Python
I am working on a project that uses Huffman algorithm to compress files, and I am doing my project using Java, what I want is to create my own file extension say (.huff) for the compressed file, and when I right click a file if it has the (.huff) extension, I want to add a new option which decompresses it, I searched the web but I did not find anything useful.
Any help would be appreciated.
To set the file extension just use one of the String methods like append(".yourExtension") (append it to the filename) and set as filename. Simple as that.
String filename = filename.append(extension);
To decompress the compressed file, I suggest you write a metod to which you provide a path to file as argument, check if the file extension is correct and then in another method you decompress this file.
There is nothing special about a file extension, it's just a part of the file name. To create a .huff file extension, just add .huff to the end of the file name.
To add the windows context menu, that's explained in the question linked in the comments How can I add a context menu to the Windows Explorer for a Java application?
I would recommend creating a batch script that will launch your program taking in the file to decompress as an argument.
Something similar to:
#echo off
java -cp <path-to-jar> <decompression main class> %1
Adding in any other setup or program arguments you need. Then a registry entry might look like.
HKEY_CLASSES_ROOT\.huff\shell\Decompress huffman encoded file\command
"<path to batch file>" "%1"
I'm trying to make an batch file that will copy the contents of a .cfg file into another .cfg file. The problem I'm having is that I want the contents of the first file to be placed at specific lines of the destination file, for example, placing the contents between line 300 and 343 and overwriting the original content within those lines.
Any way of doing this?
If there isn't a way to detect specific lines maybe there is a way to detect a specific string, like an ID?
If you are allowed to use 3rd party tools in your environment you can use a regex CLI tool to find and then replace the lines / values you need. The tool can be called using batch scripts.
Example Tools from another question:
https://superuser.com/questions/339118/regex-replace-from-command-line
I am using the libpng- a c library to check the valid .png file. If a file is valid it passes the test. I want to inject shell code in it. How can I craft a .png file, so that it is still a valid image file and also contains some shell code in it. Please tell me how is it possible. Thanks.
Well, AFAIK there is no way to inject code into a png file and execute it. But you can inject your png file into a shell script, and after view it. But you must convince the one you hack to make the png file executable and to open so-called png file through terminal.
The procedure is:
Create a text file, call it executeme.png
Paste the following code into it, note that there shouldn't be any new line at the end of the file.
#!/bin/bash
PNG_FILE=$(mktemp /tmp/hack.XXXXXXX.png)
ARCHIVE=$(awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0)
tail -n+$ARCHIVE $0 > "$PNG_FILE"
# whatever you want to do is here!
xdg-open $PNG_FILE
exit 0
__ARCHIVE_BELOW__
Append your original png file using cat injectme.png >> executeme.png.
Make executeme.png executable.
If you run the executeme.png from terminal, the original png file will be shown using the default image viewer, and your injected code will be run.
Note: I don't believe there is someone so stupid to execute that file.
Note2: On Ubuntu, executeme.png cannot be executed from file managers because it's tried to be opened using the file manager due to the png extension. You may rename file executeme.png to execute.\rpng (append a carriage return before png after dot) so at first it looks like a png file, since its extension is not png it will be executed with double click if it's executable. To make that renaming, you may need to use terminal.
Have a good time hacking! :D
Further reading: Linux journal, making installers