Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

uuencode Zip file

Thread view: 
jasonus@gmail.com - 05 Nov 2005 19:02 GMT
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
Roedy Green - 06 Nov 2005 07:08 GMT
>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

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

jasonus@gmail.com - 06 Nov 2005 23:01 GMT
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
Roedy Green - 06 Nov 2005 23:35 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

JasonDamianUs - 07 Nov 2005 16:51 GMT
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;
        }
    }
}
Roedy Green - 07 Nov 2005 16:59 GMT
>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();


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.