Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ditto zip and BOMArchiveHelper, reassemble forks and metadata



Here is some sample code on how you might want to approach calling ditto from Java. A couple of things to be aware of are:
- You might not need to keep the parent folder if you are only trying to archive a file that has a resource fork. If you are trying to archive a .app file then you will want to use the --keepParent switch.
- Strings in Java are UTF-16 and the shell is expects a UTF-8 encoding so you might have problems with files that have a path or file name that contains characters that are beyond the 7-bit ASCII characters.

Archive -------------------------------------------------------------------------------------------
StringBuilder dittoCommand = new StringBuilder();
dittoCommand.append("ditto -cj --keepParent ");
dittoCommand.append(sourceDirOrFile);
dittoCommand.append("' '");
dittoCommand.append(pathToEncodedFile);
dittoCommand.append("'");

String[] cmd = {"/bin/sh", "-c", dittoCommand.toString()};
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);

//Consume the standard error stream of this process.
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR", true);

//Consume the standard input stream of this process.
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
//Give the stream gobblers time to handle the remaining contents in the streams.
Thread.yield();
proc.destroy();
}
catch (Exception e)
{
    e.printStackTrace();
}

Unarchive -----------------------------------------------------------------------
        try
        {
             String dittoCommand = "cd '" + path + "'; ditto -x '" + fileName;
            String[] cmd = {"/bin/sh", "-c", dittoCommand};
            Runtime rt = Runtime.getRuntime();

            Process proc = rt.exec(cmd);

            //Consume the standard error stream of this process.
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR", true);

            //Consume the standard input stream of this process.
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", true);

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            //Give the stream gobblers time to handle the remaining contents in
            //the streams.
            Thread.yield();
            proc.destroy();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    public class StreamGobbler extends Thread
    {
        private InputStream is;
        private String type;

        public StreamGobbler(InputStream is, String type)
        {
            this.is = is;
            this.type = type;
        }

        public void run()
        {
            try
            {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null)
                {
                    //Do nothing.
                }
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
        }
    }

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.