Sunday, 18 August 2013

Java file list error

Java file list error

Hi i am creating an application that installs files into a selected jar. I
can't figure out how to fix the error in my list(Dir); method, It
recognizes all files and directories but when it tries to change its
directory (list(DIRECTORY);) it changes it to /originalDirectory/subFolder
i want to add a / after subFolder i've tried to do DIRECTORY + "/". But it
doesn't work any ideas? please.
My code:
public class DisplayContent extends JPanel implements ActionListener {
public DisplayContent() {
handleGraphics();
}
public String chosenDir = null;
public JButton btnInstall;
public JButton btnDone;
public boolean done = false;
private void handleGraphics() {
setLayout(null);
JLabel paneTitle = new JLabel();
paneTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
paneTitle.setBounds(55, 6, 330, 62);
paneTitle.setBackground(new Color(237, 237, 237));
paneTitle.setText("The Halo Ultimate Mod Installer");
add(paneTitle);
btnInstall = new JButton("Click Me To Install The Halo Ultimate
Mod");
btnInstall.setBounds(16, 134, 414, 47);
btnInstall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
install(chosenDir);
} catch (IOException e) {
e.printStackTrace();
}
done = true;
btnDone.setVisible(true);
btnInstall.setVisible(false);
}
});
add(btnInstall);
btnDone = new JButton("Finish Installation of Halo Ultimate Mod");
btnDone.setBounds(16, 134, 414, 47);
btnDone.setVisible(false);
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
installFinish(chosenDir);
} catch (IOException e) {
e.printStackTrace();
}
}
});
add(btnDone);
final JButton forgeFolder = new JButton("Select Forge Jar File!
Important!");
forgeFolder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chosenDir = getSelectedDir(forgeFolder);
}
});
forgeFolder.setBounds(16, 80, 414, 47);
add(forgeFolder);
}
public static String[] splitDir;
public void install(String dir) throws IOException {
JarFile Jar = new JarFile(new File(dir));
String jar = dir;
String absolutePath = new File("").getAbsolutePath();
File halo = new File(absolutePath + "/mods/");
splitDir = dir.split(".jar");
getMod(jar, halo, Jar);
System.out.print(" --- NEW DIR2 --- " + absolutePath);
System.out.print(" --- NEW DIR --- " + chosenDir);
}
public String jarName;
public String getSelectedDir(Component parent){
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode( JFileChooser.FILES_ONLY );
if( fc.showOpenDialog( parent ) == JFileChooser.APPROVE_OPTION )
{
jarName = fc.getSelectedFile().getName();
return fc.getSelectedFile().getAbsolutePath();
}
return null;
}
public void actionPerformed(ActionEvent e) {}
public void getMod(String jar, File src, JarFile jarFile) {
File folder = new File(splitDir[0] + "/assets");
try {
extractJar(jarFile, splitDir[0], folder, src);
} catch (IOException e) {
e.printStackTrace();
}
}
public void installFinish(String dir) throws IOException {
File Jar = new File(dir);
File Jar2 = new File(dir);
Jar.delete();
File Dir = new File(splitDir[0]);
File[] files = Dir.listFiles();
list(splitDir[0]);
}
public void extractJar(JarFile jar, String dest, File destCopy, File
src) throws IOException {
java.util.Enumeration enu = jar.entries();
while (enu.hasMoreElements() == true) {
java.util.jar.JarEntry file = (java.util.jar.JarEntry)
enu.nextElement();
java.io.File f = new java.io.File(dest +
java.io.File.separator + file.getName());
System.out.println(file.getName());
if(!f.exists())
{
f.getParentFile().mkdirs();
}
if (file.isDirectory()) { // if its a directory, create it
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the
input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0) { // write contents of 'is' to 'fos'
fos.write(is.read());
}
fos.close();
is.close();
}
boolean copied = false;
while (enu.hasMoreElements() == false && copied == false) {
copyFolder(src, destCopy);
copied = true;
}
}
public void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile, destFile);
}
}else{
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + "
to " + dest);
}
}
public void list(String directoryName) {
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
//files.add(file);
try {
createJar(chosenDir, chosenDir, file.getParent() +
"/");
} catch (IOException e) {
e.printStackTrace();
}
} else if (file.isDirectory()) {
list(file.getAbsolutePath() + "/" + file.getName() +
"/");
}
}
}
public boolean createJar(String jarName, String jarPath, String
subDirectoryPath)
throws IOException
{
//String jarName = jarName;
boolean status = false;
JarFile finalJarFile = null;
JarOutputStream jar = new JarOutputStream(new
FileOutputStream(jarName),new Manifest());
System.out.println(jarName + " created.");
try {
// Allocate a buffer for reading the input files.
byte[] buffer = new byte[1024];
int bytesRead;
File folder = new File(subDirectoryPath);
String fileList[] = folder.list();
// Loop through the file list.
for (int i = 1; i < fileList.length; i ++)
{
// Get the file name.
String fileName = fileList[i];
System.out.println("filename is "+fileName);
try
{
// Open the file.
FileInputStream file = new FileInputStream(subDirectoryPath +
fileName);
try
{
// Create a jar entry and add it to the jar.
JarEntry entry = new JarEntry(fileName);
jar.putNextEntry(entry);
// Read the file and write it to the jar.
while ((bytesRead = file.read(buffer)) != -1)
{
jar.write(buffer, 0, bytesRead);
}
System.out.println(entry.getName() + " added.");
}
catch (Exception ex)
{
System.out.println(ex);
}
finally
{
file.close();
}
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}
finally {
jar.close();
status = true;
System.out.println(jarName + " closed.");
}
return status;
}
}

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog.
    Really very informative post you shared here. Kindly keep blogging.
    If anyone wants to become a Java developer learn from Java Training in Chennai.
    or learn thru Java Online Training in India .
    Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete