/*
 * ZipToolApplication.java  $Revision: 1.1 $  $Date: 2007/05/12 05:56:32 $
 *
 * Copyright (c) Meldcraft, Inc. 2006
 */
package com.meldcraft.ziptool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @version $Revision: 1.1 $ $Date: 2007/05/12 05:56:32 $
 * @author Rob Richards
 */
public class ZipTool
{
    public Object[] fileOpen(ZipFile zip)
    {
        Object[] ret = null;
        if (zip != null)
        {
            ZipEntry[] entries = (ZipEntry[])Collections.list(zip.entries()).toArray(new ZipEntry[0]);
            ArrayList data = new ArrayList();
            for (int i=0 ; i < entries.length ; i++)
            {
                if (!entries[i].isDirectory())
                {
                    HashMap map = new HashMap();
                    String name = entries[i].getName();
                    String path = null;
                    int idx = name.lastIndexOf('/');
                    if (idx != -1)
                    {
                        path = name.substring(0, idx+1);
                        name = name.substring(idx+1);
                    }
                    map.put("Name", name);
                    map.put("Modified", new Date(entries[i].getTime()));
                    map.put("Size", new Long(entries[i].getSize()));
                    map.put("Packed", new Long(entries[i].getCompressedSize()));
                    map.put("Ratio", new Double((double)entries[i].getCompressedSize() / (double)entries[i].getSize()));
                    map.put("Path", path);
                    data.add(map);
                }
            }
            ret = new Object[]{zip, data};
        }
        return ret;
    }
}