hi all,this is my codes to zip and unzip.Anyone knows how to prompt the user to enter password and validate password?Can show the codes here?Thanks!It need not be a GUI.
import java.io.File;
import java.util.zip.ZipOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class CompressFile {
public static void zipFolder(String srcFolder, String destZipFile) {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
try {
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
}
catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
addFolderToZip("", srcFolder, zip);
try {
zip.flush();
zip.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private static void addToZip(String path, String srcFile,
ZipOutputStream zip) {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
}
else {
byte[] buf = new byte[1024];
int len;
try {
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ( (len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static void addFolderToZip(String path, String srcFolder,
ZipOutputStream zip) {
File folder = new File(srcFolder);
String fileListe[] = folder.list();
try {
int i = 0;
while (true) {
addToZip( /*path + "/" + */folder.getName(),
srcFolder + "/" + fileListe[i],
zip);
i++;
}
}
catch (Exception ex) {
}
}
public static final void copyInputStream(InputStream in, OutputStream out)
throws IOException
{
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public static final void main(String[] args) {
CompressFile cf = new CompressFile();
cf.zipFolder("C:
Documents and Settings\\User\\Desktop\\CompressFile",
"C:\\NewFolder
CompressFile.zip");
Enumeration entries;
ZipFile zipFile;
try {
zipFile = new ZipFile("C:\\NewFolder
CompressFile.zip");
entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
File outFile = new File("C:\\NewFolder\\UncompressFile
" + entry.getName());
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
if (entry.isDirectory()) {
System.err.println("Extracting directory: " + entry.getName());
(new File(entry.getName())).mkdir();
continue;
}
System.err.println("Extracting file: " + entry.getName());
copyInputStream(zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(new File(
"C:\\NewFolder\\UncompressFile
" + entry.getName()))));
}
zipFile.close();
}
catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
}
}
-
sample java codes to prompt password (0 messages)
- Posted by: apple low
- Posted on: August 20 2005 08:04 EDT