uuencode Zip file

Discussion in 'Java' started by jasonus@gmail.com, Nov 5, 2005.

  1. Guest

    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
     
    , Nov 5, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 5 Nov 2005 10:02:17 -0800, wrote, quoted or
    indirectly quoted someone who said :

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


    See http://mindprod.com/jgloss/armouring.html

    base64u is best for that task


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 6, 2005
    #2
    1. Advertising

  3. Guest

    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
     
    , Nov 6, 2005
    #3
  4. Roedy Green Guest

    On 6 Nov 2005 15:01:12 -0800, wrote, quoted or
    indirectly quoted someone who said :

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


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 6, 2005
    #4
  5. 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;
    }
    }
    }
     
    JasonDamianUs, Nov 7, 2005
    #5
  6. Roedy Green Guest

    On 7 Nov 2005 08:51:16 -0800, "JasonDamianUs" <>
    wrote, quoted or indirectly quoted someone who said :

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


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 7, 2005
    #6
  7. 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();
     
    JasonDamianUs, Nov 7, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ~glen~

    UUEncode/Decode

    ~glen~, Jul 20, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,508
    ~glen~
    Jul 20, 2004
  2. Replies:
    1
    Views:
    912
    Roedy Green
    Apr 13, 2006
  3. abcd
    Replies:
    7
    Views:
    529
    Edward Elliott
    May 1, 2006
  4. majj81
    Replies:
    0
    Views:
    341
    majj81
    May 31, 2006
  5. Chris Lambacher

    Re: Add file to zip, or replace file in zip

    Chris Lambacher, Jun 1, 2006, in forum: Python
    Replies:
    0
    Views:
    618
    Chris Lambacher
    Jun 1, 2006
Loading...

Share This Page