Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

ZipFolder Using Java By Format Date, Java Example Java Io ZipFolderExample

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

;
 
Java Examples for Beginners
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

package com.smedia.filetranfer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* Created by IntelliJ IDEA.
* User: The Hien
* Date: Sep 22, 2009
* Time: 2:50:10 PM
*/
public class ZipFolder {
public static void main(String arg[]) throws Exception {
int dayBefore = 1;

String srcFolder = "C:\\Documents and Settings\\The Hien\\Desktop\\LogXS";


Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -dayBefore);
// SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");

/*Tim thu muc ngay hom truoc se la: 102009/18102009*/

DateFormat dateFormat = new SimpleDateFormat("/MMyyyy/ddMMyyyy");
srcFolder = srcFolder + dateFormat.format(calendar.getTime());

String destZipFile = srcFolder + ".zip";

File file = new File(srcFolder);
if (file.exists() && file.isDirectory()) ;

zipFolder(srcFolder, destZipFile);
(new SendFile()).fileTranfer();
DelFolder.deleteDir(new File(srcFolder));

/* Nen folder 6/9/2009-> Path folder: 092009/06092009
Kiem tra folder ton tai , neu ton tai thi zip thu muc 06092009-> 06092009.zip
Gui len ftp server vao thu muc [desdir]/092009/
*/
}

static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
boolean ziped = false;
try {
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);

addFolderToZip("", srcFolder, zip);
ziped = true;
System.out.println("Your file " + (ziped ? "" : "not ") + "zipped .");
}

finally {
if (null != zip) zip.close();
if (null != fileWriter) fileWriter.close();

}
}

static private void addFileToZip(String Path, String scrFile, ZipOutputStream zip) throws Exception {

File folder = new File(scrFile);
if ((folder.isDirectory())) {
addFolderToZip(Path, scrFile, zip);
} else {

byte[] buffer = new byte[1024];
int len;
FileInputStream in = null;
try {
in = new FileInputStream(scrFile);
zip.putNextEntry(new ZipEntry(Path + "/" + folder.getName()));

/* putNextEntry(new ZipEntry(filesToZip)) l m?t ph??ng th?c c?a l?p ZipOutputStream
dng ?? ??a vo t?ng file m?t ?? nn l?i.*/

while ((len = in.read(buffer)) > 0) {
zip.write(buffer, 0, len);
}
}
finally {
if (null != in) in.close();
}
}
}

static private boolean addFolderToZip(String subFolder, String srcFolder, ZipOutputStream zip)
throws Exception {

File folder = new File(srcFolder);
try {
for (File item : folder.listFiles()) {
if (item.isDirectory()) addFolderToZip(subFolder + "/" + item.getName(), item.getPath(), zip);
else {
addFileToZip(subFolder, item.getPath(), zip);
}
}

return true;
} catch (Exception e) {
e.printStackTrace();

}
return false;
}
}

Posted by Hien on Thursday, 12.3.09 @ 08:22am | #93035

Ask programming questions?

 

 

Back to Tutorial
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.