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;
}
}
}
>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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
JasonDamianUs - 07 Nov 2005 19:23 GMT
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();