create tar file programatically in java - java

I'm trying to convert a directory into tar file, which includes an empty sub-directory and sub-directories with some .json file, using Java's JTar. Folders that contain files are automatically included in the .tar file, but the empty folders are not.I want to create .tar file with all(empty and with data init) the sub directories.
My code is this way:
try
{
File tarFile = new File("somefilename.tar");
TarOutputStream tos = new TarOutputStream(new FileOutputStream(tarFile));
tartar("directoryname", tos);
tos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
private static void tartar(String dir, TarOutputStream tos)
{
File f = new File(dir);
String[] flist = f.list();
int buffersize = 1024;
byte[] buf = new byte[buffersize];
for (int i = 0; i < flist.length; i++)
{
File f2 = new File(f, flist[i]);
if (f2.isDirectory())
{
tartar(f2.getPath(), tos);
continue;
}
try
{
FileInputStream fis = new FileInputStream(f2);
// TarEntry te = new TarEntry(f2.getPath());
TarEntry te = new TarEntry(f2, f2.getPath());
tos.putNextEntry(te);
int count = 0;
while ((count = fis.read(buf, 0, buffersize)) != -1)
{
tos.write(buf, 0, count);
}
// tos.closeEntry();
// tos.close();
fis.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

While I have not worked with invoking tarball in Java, but in Linux you can run command "tar -cvzf file_name.tar.gz directory_name/" using Runtime, Process and ProcessBuilder API.

Related

How to get .png files alone from one folder

Actually, i'm trying to zip all the files from one folder & .png files from another folder. I can able to get all the files from one folder. But i can't able to get the .png files from another folder in java. Is there any way ?
Code:
public class Zip {
public static void zip(String filepath,String reportFileName){
try {
File inFolder=new File(filepath);
File inFolder1=new File("../Agent_Portal_Auto_Testing/ReportCharts");
File outFolder=new File(reportFileName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFolder)));
BufferedInputStream in = null;
byte[] data = new byte[1000];
String files[] = inFolder.list();
String files1[]=inFolder1.list();
for (int i=0; i<files.length; i++) {
in = new BufferedInputStream(new FileInputStream
(inFolder.getPath() + "/" + files[i]), 1000);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while((count = in.read(data,0,1000)) != -1) {
out.write(data, 0, count);
}
}
for (int i=0; i<files1.length; i++) {
in = new BufferedInputStream(new FileInputStream
(inFolder1.getPath() + "/" + files1[i]), 1000);
out.putNextEntry(new ZipEntry(files1[i]));
int count;
while((count = in.read(data,0,1000)) != -1) {
out.write(data, 0, count);
}
}
out.closeEntry();
out.flush();
out.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
In the above code, i'm getting all the files from ReportCharts folder. But i need to get only the .png files.
See http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles(java.io.FileFilter)
You can use the file filter to filter out only the PNG files
http://docs.oracle.com/javase/7/docs/api/java/io/FileFilter.html
File [] pngFiles = directory.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile() && file.getName().toLowerCase().endsWith(".png");
}
});
you can add verify if you file is a .png one with :
if (files1[i].contains(".png"))
in your for loop.

java unzipping files with folders

First of all, I didn't see any other question that helps me 'directly' with my problem, that's why I decided to create a new one.
The following code is programmed to unzip multiple files and folders.
try {
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(file);
CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
String rutaarchivo = entry.getName();
int index= rutaarchivo.lastIndexOf('/');
rutaarchivo=rutaarchivo.substring(index+1);
estado.setText("Estado: Copiando archivos nuevos: " + entry);
setProgress(60);
int zipCounter = 0;
int count;
int BUFFER = 2048;
byte datazip[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(rutaarchivo);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(datazip, 0, BUFFER)) != -1) {
dest.write(datazip, 0, count);
zipCounter += count;
progreso.setValue(zipCounter);
}
dest.flush();
dest.close();
}
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
PrintStream outprint = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(outprint);
} catch (MalformedURLException ex) {
ex.printStackTrace();
PrintStream outprint = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(outprint);
} catch (IOException ioe) {
ioe.printStackTrace();
PrintStream outprint = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(outprint);
}
This code is working right but has an important mistake: it does uncompress folders, but instead of showing them like this:
file1.txt
file2.txt
file3.txt
folder/file4.txt
folder/file5.txt
folder/file6.txt
It shows this:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
What am I doing wrong?
You're setting the name of the file in the FileOutputStream to just the file name.
String rutaarchivo = entry.getName();
int index= rutaarchivo.lastIndexOf('/'); // removes directory path
rutaarchivo=rutaarchivo.substring(index+1); // removes directory path
...
FileOutputStream fos = new FileOutputStream(rutaarchivo);
This is a bad idea. You should make your own base file path and append a file name to that.

Compressing a folder into a ZipFile

I have this Java method to upload a file. I am trying to cater for users trying to upload a folder by compressing that folder into a zip file and upload it instead. For some reason in my case file.isDirectory() and file.isFile() are not working correctly.. even though the filename does not contain any extension, file.isFile() is returning true and isDirectory() returns false. Also directory.list() is also acting weird by returning null.
What can be the problem? Am I doing something wrong?
public File uploadFile(FileItem item, String filename, int ticket_id) throws IOException
{
FileOutputStream out = null;
InputStream fileContent = null;
File file = null;
try
{
//fullpath returns C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test
StringBuffer fullPath = new StringBuffer();
fullPath.append(Attachment.attachments_path);
fullPath.append("t");
fullPath.append(Integer.toString(ticket_id));
fullPath.append(File.separator);
fullPath.append(filename);
System.out.println("filename " + filename);
file = new File(fullPath.toString());
if (!file.exists())
{
// if directory does not exist, create it
file.getParentFile().mkdirs();
}
if (file.isFile())
{
// if file is not a folder
out = new FileOutputStream(file);
fileContent = item.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
// read all the file and write it to created file
while ((read = fileContent.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
}
else if (file.isDirectory())
{
ZipFile appZip = new ZipFile(fullPath.toString());
appZip.generateFileList(file);
appZip.zipIt(filename + ".zip");
}
}
catch (FileNotFoundException e)
{
LogFile.logError("[FileUpload.uploadFile()] " + e.getMessage());
}
catch (IOException e1)
{
LogFile.logError("[FileUpload.uploadFile()] " + e1.getMessage());
}
finally
{
if (out != null)
{
out.close();
}
if (fileContent != null)
{
fileContent.close();
}
}
return file;
}
This is the ZipFile class I am using
public class ZipFile
{
List<String> fileList = null;
String source_folder = "";
public ZipFile(String source_folder)
{
fileList = new ArrayList<String>();
this.source_folder = source_folder;
}
public void zipIt(String zipFile)
{
byte[] buffer = new byte[1024];
String source = "";
try
{
try
{
source = source_folder.substring(source_folder.lastIndexOf("\\") + 1, source_folder.length());
}
catch(Exception e)
{
source = source_folder;
}
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String file : this.fileList)
{
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(source_folder + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
//remember close it
zos.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
public void generateFileList(File node)
{
// add file only
if(node.isFile())
{
fileList.add(generateZipEntry(node.toString()));
}
if(node.isDirectory())
{
String[] subNode = node.list();
if (subNode != null) {
for(String filename : subNode)
{
generateFileList(new File (node, filename));
}
}
}
}
private String generateZipEntry(String path)
{
return path.substring(source_folder.length() + 1, path.length());
}
}
file.list() is being done in the generateFileList method in ZipFile class. I know this is returning null since I tried detecting whether the file is a folder or a file by using filename.indexOf(".") instead of isDirectory() and isFile() since they were not working. But I wish I had an explanation for this.
Thanks for your help!
if (!file.exists()) {
// if directory does not exist, create it
file.mkdirs();
}
will create directory and test file.isDirectory() will return true
It could be a problem with the path?
C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test
You are mixing backslash with slash...
I tested your code block
ZipFile appZip = new ZipFile(file.toString());
appZip.generateFileList(file);
appZip.zipIt(filename + ".zip");
with a local folder and it's working perfectly. I think you are passing a invalid path. This may be the cause isFile or isDirectory methods are acting strangely. Try to add a validation statement at the starting of generateFileList method using File API:
if(!node.exists) {
// return some flag to signify error OR throw a suitable Exception
}
This should work.
public String compressData(String srcDir) {
String zipFile = srcDir+".zip";
try {
// create byte buffer
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
File dir = new File(srcDir);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println("Adding file: " + files[i].getName());
FileInputStream fis = new FileInputStream(files[i]);
// begin writing a new ZIP entry, positions the stream to the start of the entry data
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
// close the InputStream
fis.close();
}
// close the ZipOutputStream
zos.close();
}
catch (IOException ioe) {
System.out.println("Error creating zip file" + ioe);
}
return zipFile;
}

I am not able to set the path of the class file which I am adding in the jar through code

public static int compileModifiedClass(String ModifiedFile)
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, ModifiedFile);
System.out.println("Compile result code = " + result);
return result;
}
Path for ModifiedFile: D:\ModifiedJavaFiles\com\example\tests\
So, the classes are generated in this folder and Jar path is :D:\test.jar
I want to set the path for these classes in the jar as com/example/tests.
Any help would really be appreciated.
To add the info, after compiling the classes, I am getting the classes from the path and calling updateZipFile method to update the jar.
classes = getClassesFromPath(ModifiedFilesPath, JarPath);
File jarFile = new File(JarPath);
JarUpdater jarUpdater = new JarUpdater();
try
{
jarUpdater.updateZipFile(jarFile, classes);
}
catch (IOException e)
{
e.printStackTrace();
}
public void updateZipFile(File zipFile,
File[] files) throws IOException {
// get a temp file
File tempFile = File.createTempFile(zipFile.getName(), null);
// delete it, otherwise you cannot rename your existing zip to it.
tempFile.delete();
boolean renameOk=zipFile.renameTo(tempFile);
if (!renameOk)
{
throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
}
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
String name = entry.getName();
boolean notInFiles = true;
for (File f : files) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Compress the files
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(files[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
tempFile.delete();
}
URL jar = jarFile.toURI().toURL();
URL dir = new URL("file:/D:/ModifiedJavaFiles/");
URL[] urls = { jar, dir };
URLClassLoader loader = new URLClassLoader(urls);
Class<?> klazz = loader.loadClass("com.example.tests.MyTest");
loader.close();

Issues with zipping files in Java

I am currently trying to zip all files within a directory.
The zip file is being created and the files are being processed - but for some reason the files are not appearing within the zip file.
The code being used to complete this task is as follows:
public class FileZipper {
public void zipDir( String dir, String zipFileName ) {
try{
File dirObj = new File(dir);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
Logger.info("Creating : " + zipFileName);
addDir(dirObj, out);
out.close();
}
catch (Exception e){
Logger.error( e, "Error zipping directory" );
}
}
private void addDir(File dirObj, ZipOutputStream out) throws IOException {
File[] files;
if( !dirObj.isDirectory() ){
files = new File[] { dirObj };
}
else{
files = dirObj.listFiles();
}
byte[] tmpBuf = new byte[1024];
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDir(files[i], out);
continue;
}
FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
Logger.info(" Adding: " + files[i].getAbsolutePath());
out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
int len;
while ((len = in.read(tmpBuf)) > 0) {
out.write(tmpBuf, 0, len);
}
out.closeEntry();
in.close();
}
}
}
When reviewing the logging information, the files within the directories are being found and processed, but the created zip file contains no data.
Any help with this issues will be greatly appreciated.
Thanks
Apart from the fact that adding the file by its absolute path is probably not what you want, this code works just fine for me.
Hy,
Give a set of files name to this function, and a zip name.
It should work.
private void zipFiles (ArrayList<String> listWithFiles, String zipName) {
try {
byte[] buffer = new byte[1024];
// create object of FileOutputStream
FileOutputStream fout = new FileOutputStream(zipName);
// create object of ZipOutputStream from FileOutputStream
ZipOutputStream zout = new ZipOutputStream(fout);
for (String currentFile : listWithFiles) {
// create object of FileInputStream for source file
FileInputStream fin = new FileInputStream(currentFile);
// add files to ZIP
zout.putNextEntry(new ZipEntry(currentFile ));
// write file content
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
// close the InputStream
fin.close();
}
// close the ZipOutputStream
zout.close();
} catch (IOException ioe) {
System.out.println("IOException :" + ioe);
}
}
All good to you,
dAN

Categories