javafile-uploadzipuuencode

uuencode zipfile in java


I am a novice user trying to figure out how to use uuencode method. We have a form that allows only a single text file to be uploaded. Now it looks like only zip files will be uploaded. I am trying to include uuencode method to convert bytes to String so that we dont have to modify the rest of the code to accommodate the binary file.

Original code:

public void SettingUpload(File inputfile) { 
    this.inputfile = inputfile;
}

I changed it to

public void SettingUpload(File inputfile){
UUEncoder uuec = new UUEncoder();
    try{
        InputStream is = new FileInputStream(inputfile);
        OutputStream os = new FileOutputStream(inputfile);
        uuec.encodeBuffer(is, os);
        this.inputfile = inputfile;
    }catch (Throwable error) {
        reportError(error, "Error converting zipfile");
    }

}

When I tested it out, I got a java.io.EOFException. I grabbed the uuencoded file and manually uudecoded it. When I tried to unzip it,

bash1:~>unzip s6b0c9e663c74f72941bd8271a5fac3b.bin 
 Archive:  s6b0c9e663c74f72941bd8271a5fac3b.bin

 End-of-central-directory signature not found.  Either this file is not
 a zipfile, or it constitutes one disk of a multi-part archive.  In the

Edit:

I changed it to:

 public void SettingUpload(File inputfile){
    UUEncoder uuec = new UUEncoder();
        try{
            InputStream is = new FileInputStream(inputfile);
                   File OutputFile=new File("Output");
                        OutputFile.createNewFile();
            OutputStream os = new FileOutputStream(OutputFile);
            uuec.encodeBuffer(is, os);
            this.OutputFile = OutputFile;
        }catch (Throwable error) {
            reportError(error, "Error converting zipfile");
        }

 }

and I get the following error:

cannot find symbol
symbol  : variable OutputFile

Solution

  • As Haozhun has commented, you shouldn't do this:

    InputStream is = new FileInputStream(inputfile); // Good
    OutputStream os = new FileOutputStream(inputfile); // Bad
    

    You should be outputting to a separate file, otherwise the first time you write, you will trash the original file.

    UPDATED with example

    This works fine for me...

    public static void main(String[] args) {
    
        File inputfile = new File("file/to/be/encoded");
        File outFile = new File("out.uue");
    
        UUEncoder uuec = new UUEncoder();
        InputStream is = null;
        OutputStream os = null;
        try {
    
            is = new FileInputStream(inputfile);
            os = new FileOutputStream(outFile);
            uuec.encodeBuffer(is, os);
    
        } catch (Exception error) {
            error.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
            }
            try {
                os.close();
            } catch (Exception e) {
            }
        }
    
        File newFile = new File("decoded.jpg");
        UUDecoder decoder = new UUDecoder();
        try {
    
            is = new FileInputStream(outFile);
            os = new FileOutputStream(newFile);
            decoder.decodeBuffer(is, os);
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
            }
            try {
                os.close();
            } catch (Exception e) {
            }
        }
    
    }
    

    Also, I would return the output file from you encode methods

    public void SettingUpload(File inputfile) throws IOException {
        UUEncoder uuec = new UUEncoder();
        File outFile = File.createTempFile("encoded", "uue");
        InputStream is = null;
        OutputStream os = null;
        try{
            is = new FileInputStream(inputfile);
            os = new FileOutputStream(outFile );
            uuec.encodeBuffer(is, os);
        } finally {
            try {
                is.close();
            } catch (Exception e) {
            }
                try {
            os.close();
            } catch (Exception e) {
            }
        }
        return outFile;
    }
    

    You should never suppress exceptions. How would the caller know if something has gone wrong?

    Also, if you open a stream, you're responsible for closing it, so make sure you're closing your streams.