Skip to content

Instantly share code, notes, and snippets.

@jedi4ever
Created April 13, 2011 08:30
Show Gist options
  • Save jedi4ever/917196 to your computer and use it in GitHub Desktop.
Save jedi4ever/917196 to your computer and use it in GitHub Desktop.
using fat-32 lib: create a new floppy img from all files in a directory (non recursive)
package be.jedi.dir2floppy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import de.waldheinz.fs.FileSystem;
import de.waldheinz.fs.FsDirectoryEntry;
import de.waldheinz.fs.FsFile;
import de.waldheinz.fs.fat.SuperFloppyFormatter;
import de.waldheinz.fs.util.FileDisk;
/**
* Create floppy img from files in a directory
*
*/
public class Dir2Floppy
{
public static void main( String[] args )
{
if (args.length < 2) {
System.out.println("Usage: java -jar dir2floppy.jar <sourcedir> <floppyfile>");
System.exit(-1);
}
FileDisk device = null;
//Create the floppy
try {
device = FileDisk.create(new File(args[1]),(long)1440 * 1024);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
//Format the floppy
FileSystem fs=null;
try {
fs = SuperFloppyFormatter.get(device).format();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
//Iterate of directories
File dir = new File(args[0]);
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
System.out.println("Error. does the directory exist?");
System.exit(-1);
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
File aFile=new File(dir.getAbsolutePath()+System.getProperty("file.separator")+children[i]);
try {
// Create the entry on the floppy
FsDirectoryEntry floppyEntry = fs.getRoot().addFile(children[i]);
//floppyEntry.setName(children[i]);
System.out.print("- Processing file: "+children[i]+" ");
FsFile floppyfile = floppyEntry.getFile();
// Copy the file over
if (aFile.isFile()) {
FileInputStream fis= new FileInputStream(aFile);
FileChannel fci = fis.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
long counter=0;
int len;
// http://www.kodejava.org/examples/49.html
// Here we start to read the source file and write it
// to the destination file. We repeat this process
// until the read method of input stream channel return
// nothing (-1).
while(true)
{
// read a block of data and put it in the buffer
int read = fci.read(buffer);
// did we reach the end of the channel? if yes
// jump out the while-loop
if (read == -1)
break;
// flip the buffer
buffer.flip();
// write to the destination channel
System.out.print(".");
floppyfile.write(counter*1024, buffer);
counter++;
// clear the buffer and user it for the next read
// process
buffer.clear();
}
System.out.println();
floppyfile.flush();
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "Done" );
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.jedi</groupId>
<artifactId>dir2floppy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dir2floppy</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>de.waldheinz</groupId>
<artifactId>fat32-lib</artifactId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment