03-12-2010, 05:57 PM
This is an advanced packer which packs files into either the Zip, JAR or GZIP package formats. This is a bit self explanatory so no need for an explanation.
http://projectevolution.comoj.com/produc.../ePack.jar
Src - PackageUI
Src - PackageImpl
http://projectevolution.comoj.com/produc.../ePack.jar
Src - PackageUI
Code:
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
/*
* PackageUI.java
*
* Created on 12-Mar-2010, 11:43:42 AM
*
* @author Anthony Calandra
*/
public class PackageUI extends javax.swing.JFrame {
public static void main(String[] args) {
new PackageUI().setVisible(true);
}
public PackageUI() {
super("e-Pack - Project Evolution");
initComponents();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
DLM = new DefaultListModel();
jList1 = new javax.swing.JList(DLM);
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton5 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("ePack - Project Evolution");
jScrollPane1.setViewportView(jList1);
jRadioButton1.setSelected(true);
jRadioButton1.setText("Compress - ZIP");
jRadioButton2.setText("Compress - JAR");
jRadioButton3.setText("Compress - GZIP");
jButton1.setText("Compress List");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Compress Element");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Clear List");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Remove Element");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
jButton5.setText("Browse");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jTextField1.setEditable(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton3)
.addComponent(jRadioButton2))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3))))))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(jLabel1)))
.addContainerGap())
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jRadioButton1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jRadioButton2)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton3)
.addComponent(jButton3)
.addComponent(jButton4))
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new PackageImpl(PackageImpl.queuedFiles);
if (!DLM.isEmpty())
jTextArea1.append("Successfully packaged file(s).\n");
PackageImpl.queuedFiles.clear();
DLM.clear();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
file = fileChooser.getSelectedFile();
new PackageImpl(file.getName(), getSelection());
if (!DLM.isEmpty())
jTextArea1.append("Successfully packaged file.\n");
PackageImpl.queuedFiles.remove(indexElementString(file.getName()));
DLM.remove(jList1.getSelectedIndex());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
PackageImpl.queuedFiles.clear();
DLM.clear();
jTextArea1.append("Cleared package list.\n");
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
PackageImpl.queuedFiles.remove(indexElementString(file.getName()));
DLM.remove(jList1.getSelectedIndex());
jTextArea1.append("Removed - "+file.getName()+" .\n");
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
int pos = jList1.getModel().getSize();
fileChooser.showOpenDialog(this);
file = fileChooser.getSelectedFile();
DLM.add(pos, file.getName());
PackageImpl.queuedFiles.put(file.getName(), getSelection());
jTextField1.setText(file.getPath());
jTextArea1.append("Added - "+file.getName()+" .\n");
}
private int indexElementString(String name) {
for (int i = 0; DLM.getSize() > i; i++) {
if (DLM.contains(name))
return DLM.indexOf(i);
}
return 0;
}
private PackageImpl.Extensions getSelection() {
if (jRadioButton1.isSelected())
return PackageImpl.Extensions.ZIP;
if (jRadioButton2.isSelected())
return PackageImpl.Extensions.JAR;
if (jRadioButton3.isSelected())
return PackageImpl.Extensions.GZIP;
return PackageImpl.Extensions.ZIP;
}
File file;
DefaultListModel DLM;
JFileChooser fileChooser = new JFileChooser();
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
}
Src - PackageImpl
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/*
* PackageImpl.java
*
* Created on 12-Mar-2010, 11:43:42 AM
*
* @author Anthony Calandra
*/
public class PackageImpl {
public static HashMap<String, Extensions> queuedFiles = new HashMap<String, Extensions>();
public enum Extensions { ZIP, JAR, GZIP };
public PackageImpl(HashMap<String, Extensions> files) {
Set set = files.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry map = (Map.Entry)i.next();
Pack((String) map.getKey(), (Extensions) map.getValue());
}
}
public PackageImpl(String file, Extensions extension) {
Pack(file, extension);
}
public void Pack(String filePath, Extensions extension) {
String ext = filePath;
int len;
byte[] buf = new byte[1024];
Extensions extensions = extension;
try {
FileInputStream in = new FileInputStream(filePath);
ext = getExtension(ext);
switch (extensions) {
case ZIP:
ZipOutputStream ZipOut = new ZipOutputStream(new FileOutputStream(ext + ".zip"));
ZipOut.putNextEntry(new ZipEntry(filePath));
while ((len = in.read(buf)) > 0)
ZipOut.write(buf, 0, len);
ZipOut.closeEntry();
ZipOut.close();
break;
case JAR:
JarOutputStream JarOut = new JarOutputStream(new FileOutputStream(ext + ".jar"));
JarOut.putNextEntry(new ZipEntry(filePath));
while ((len = in.read(buf)) > 0)
JarOut.write(buf, 0, len);
JarOut.closeEntry();
JarOut.close();
break;
case GZIP:
GZIPOutputStream GZipOut = new GZIPOutputStream(new FileOutputStream(ext + ".gz"));
while ((len = in.read(buf)) > 0)
GZipOut.write(buf, 0, len);
GZipOut.close();
break;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String getExtension(String ext) {
String regex = "^.+\\.((gz)|(dat)|(zip)|(jar)|(txt)|(java)|(class)|(jpg)|(png))$";
boolean matches = ext.matches(regex);
if (matches)
return ext.replaceAll("\\.((gz)|(dat)|(zip)|(jar)|(txt)|(java)|(class)|(jpg)|(png))$", "");
return "";
}
}