//ZipME Package
ZipME Package Dowload Link
import net.sf.zipme.ZipInputStream;
import net.sf.zipme.ZipEntry;
public void zip(String filePath, int BUFFER) {
this.url=filePath;
javax.microedition.io.Connection c = null;
FileConnection con = null;
//Create RAR Directory
try {
String URL = "file:///root1/";
String rootDir = "file:///root1/RAR/Sample.txt";
// String rootDir = "file:///e:/Other/rar/epub.txt";
System.out.println("Create directory:Entry" + URL);
String name = "RAR";
URL.endsWith("/");
alwaysURL = "";
con = (FileConnection) javax.microedition.io.Connector.open(URL + name);
con1 = (FileConnection) javax.microedition.io.Connector.open(rootDir);
if (!(con.exists() && con.isDirectory())) {
con.mkdir();
System.out.println("Create Directory");
rootDir = con.getName();
// Try to create the directory, warn if it fails
System.out.println("Creating Directory: " + rootDir);
} else {
System.out.println("Always Available");
}
if (!(con1.exists() && con1.isDirectory())) {
con1.create();
}
root = URL + rootDir;
} catch (Exception e) {
}
//Rar or Zip File Extraction
try {
InputStream is = readRarFile(url);
ZipInputStream zis = new ZipInputStream(is);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
int i = (int) ze.getSize();
b = new byte[i];
//Zip File Readinn Process
writeFile(zis, ze.getName(), b, root);
zis.closeEntry();
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//Fetch Zip File
public InputStream readRarFile(String url) {
javax.microedition.io.Connection c = null;
java.io.InputStream is = null;
try {
c = javax.microedition.io.Connector.open(url, javax.microedition.io.Connector.READ);
javax.microedition.io.file.FileConnection fc = (javax.microedition.io.file.FileConnection) c;
is = fc.openInputStream();
} catch (Exception e) {
System.out.println("Error in reding the File");
}
return is;
}
//Load Zip or Rar File after Extraction
public boolean writeFile(ZipInputStream zis, String path, byte[] b, String URL2) {
try {
System.out.println(" new Directory" + URL2);
System.out.println("filename:" + path);
javax.microedition.io.Connection c = null;
String url = URL2 + path;
System.out.println("url:" + url);
try {
System.out.println("Enter the writing process");
conn = (FileConnection) javax.microedition.io.Connector.open(url);
// Write the File
if (url.startsWith("/")) {
if (!warnedMkDir) {
System.out.println("Ignoring absolute paths");
}
warnedMkDir = true;
url = url.substring(1);
}
// if a directory, just return. We mkdir for every file,
// since some widely-used Zip creators don't put out
// any directory entries, or put them in the wrong place.
if (url.endsWith("/")) {
return false;
}
// Else must be a file; open the file for output
// Get the directory part.
int ix = url.lastIndexOf('/');
if (ix > 0) {
dirName = url.substring(0, ix);
// if (!dirsMade.contains(dirName))
if (dirsMade != dirName) {
conn1 = (FileConnection) javax.microedition.io.Connector.open(dirName);
// If it already exists as a dir, don't do anything
if (!(conn1.exists() && conn1.isDirectory())) {
conn1.mkdir();
createFiles(zis);
// Try to create the directory, warn if it fails
System.out.println("Create Directory: " + dirName);
if (!(conn1.isDirectory())) {
System.err.println("Warning: unable to mkdir " + dirName);
}
dirsMade.equals(dirName);
}
}
}
createFiles(zis);
} catch (IOException e) {
// error
} catch (SecurityException e) {
// no permission to create/write
}
conn.close();
return true;
} catch (Exception e) {
str = str + e.toString();
return false;
} finally {
try {
if (os != null) {
os.close();
}
// if (conn!= null)
// conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void createFiles(ZipInputStream zis) throws IOException {
if (!conn.exists() && !conn.isDirectory()) {
conn.create();
} else {
conn.truncate(0);
}
os = conn.openOutputStream();
//Write Files
int count;
while ((count = zis.read(b, 0, b.length)) != -1) {
os.write(b, 0, count);
}
os.flush();
}