uuencode Zip file

J

jasonus

Anyone have an example of uuencoding/decoding a zip file?

I am tryin to pass it over across http and need to convert the bytes
in it.

Have been trying to use javax.mail.internet.MimeUtility with no luck.

thanks

Jason
 
J

jasonus

i dont have the luxury of using a custom package (only certain jars are
deployed on the servre), and there are also licensing issues. can this
be done using javamail?

Jason
 
R

Roedy Green

i dont have the luxury of using a custom package (only certain jars are
deployed on the servre), and there are also licensing issues. can this
be done using javamail?

Java mail is for mail not plain http. Further javamail is a big jar
you have to deploy. Base64 or Base64u is a tiny little class. You can
put it in an signed Applet or a JAWS utility to prepare the file at
the client or to unpack it from the server.

HTTP is happy to send raw binary. Consider how many thousands of files
are downloaded every day. Maybe you don't even need armouring.
 
J

JasonDamianUs

So, here is the code I am using with your package.. it still converts a
few characters incorrectly. What am I doing wrong. Any help is greatly
apprecaited.

Specifically

81 -> 3F
8D -> 3F
8F -> 3F
90 -> 3F


public class Test
{

public static final String zipCharset = "ISO-8859-1";
public static final String utf8 = "ISO-8859-1";
public static void main(String[] args)
{
String in = readFile(new File("c:\\in.zip"));
Base64u base64u = new Base64u();
base64u.setLineLength( 72 ); // default

String enc = base64u.encode(in.getBytes());
// System.out.println(enc);
String dec = new String(base64u.decode(enc));
writeToFile(new File("c:\\out.zip"), dec);
}

/*
* Write output string to a file, encode if wanted
*/
public static void writeToFile(File f, String str) {
try {
OutputStream fos;
Writer out;
fos = new FileOutputStream(f);
out = new OutputStreamWriter(fos);
out.write(str);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}


/*
* Read input string from a file, decode if wanted
*/
public static String readFile(File f) {

StringBuffer buffer = new StringBuffer();
FileInputStream fis;
InputStreamReader isr;

try {
fis = new FileInputStream(f);;
isr = new InputStreamReader(fis);

Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
in.close();
return buffer.toString();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
 
R

Roedy Green

String in = readFile(new File("c:\\in.zip"));
Base64u base64u = new Base64u();

if you convert a zip file to a string you will totally mangle it. You
must read the zip file into a byte array before Base64u can encode it.

See http://mindprod.com/applets/fileio.html

tell it you want to read raw bytes, unbuffered from a file.

Then read the entire file into a byte array File.length() bytes long
in one fell swoop.

Then feed that to Base64u.
 
J

JasonDamianUs

Makes sense. But the interesting thing is that when I used a string
buffer and wrote the file back immediately - it produced a valid zip.

File inFile = new File(inputFilename);
FileInputStream fis = new FileInputStream(inFile);
InputStreamReader r = new InputStreamReader(fis, zipEncoding);
Reader in = new BufferedReader(r);
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
String inputFileAsString = buffer.toString();

// write output directly to file - WORKS as zip
File outfile = new File(decodedFileName);
FileOutputStream fos = new FileOutputStream(outfile);
Writer out = new OutputStreamWriter(fos, zipEncoding);
out.write(inputFileAsString);
out.close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top