import java.util.*;
import java.io.*;
import org.apache.commons.compress.archivers.zip.*;
import org.apache.commons.compress.utils.IOUtils;
public void unzip(String ziplocation, String zipname, String unziplocation){
//method variables
private ZipFile zipFile;
private Enumeration<ZipArchiveEntry> entries;
private ZipArchiveEntry entry;
private File entryDestination;
private InputStream in;
private OutputStream out;
zipFile = new ZipFile(ziplocation+File.separator+zipname);
//get zip file content references
entries = zipFile.getEntries();
if(entries != null){
//for each reference, unzip to unziplocation, creating directories if needed
while (entries.hasMoreElements()) {
entry = entries.nextElement();
entryDestination = new File(unziplocation, entry.getName());
entryDestination.getParentFile().mkdirs();
if (entry.isDirectory()){
entryDestination.mkdirs();
}
else {
in = zipFile.getInputStream(entry);
out = new FileOutputStream(entryDestination);
IOUtils.copy(in, out);
//close streams ignoring exceptions
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
}
}
Pages
▼
30/08/2014
[Java] unzip
Now that we know how to zip in Java, let's see how to unzip using the same Apache Commons Compress library:
[Java] zip file or directory (with subdirectories)
Note: it is possible to perform zip operation with standard Java libraries, but more superfluous steps are required (ie: more streams have to be handled).
A simple way to zip a file or folder (with or without subdirectories) maintaining the folders structure is using the Apache Commons Compress library.
It has to be recursive so that we can handle subdirectories correctly. The resulting zipped file will unzip to the same exact folder structure originally zipped. If you pass the location and ziplocation parameters with the path separator already appended, there's no need to concatenate File.separator in the code.
A simple way to zip a file or folder (with or without subdirectories) maintaining the folders structure is using the Apache Commons Compress library.
import java.util.*;
import java.io.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.archivers.*;
import org.apache.commons.compress.archivers.zip.*;
public void zip(String location, String name, String ziplocation, String zipname){
//method variables
private OutputStream out;
private ArchiveOutputStream zip_out;
private File f;
//out writes the final file, zip_out creates the zip archive
out = new FileOutputStream(new File(ziplocation+File.separator+zipname+".zip"));
zip_out = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
//zip it
File f = new File(location+File.separator+name);
//first time baseDir is empty
dozip(f, "");
//close archive
zip_out.finish();
out.close();
}
//aux method for zipping
private void dozip(File myFile, String baseDir) throws Exception{
//maintain the directory structure while zipping
String entryName = baseDir+myFile.getName();
//DO NOT do a putArchiveEntry for folders as it is not needed
//if it's a directory then list and zip the contents. Uses recursion for nested directories
if(myFile.isDirectory() == true){
File[] filesList = myFile.listFiles();
if(filesList != null){
for (File file : filesList) {
dozip(file, entryName+File.separator);
}
}
}
else{
//add file
zip_out.putArchiveEntry(new ZipArchiveEntry(myFile, entryName));
IOUtils.copy(new FileInputStream(myFile), zip_out);
zip_out.closeArchiveEntry();
}
}
It has to be recursive so that we can handle subdirectories correctly. The resulting zipped file will unzip to the same exact folder structure originally zipped. If you pass the location and ziplocation parameters with the path separator already appended, there's no need to concatenate File.separator in the code.
[Java] move file or directory
Java versions before 7 amazingly lack a simple (one method) way of moving a file or directory to another directory.
Luckily there's the Apache Commons IO library. Using that, you would do something like:
import java.util.*;
import java.io.*;
import org.apache.commons.io.FileUtils;
public void move(String fromDir, String toDir){
//moveToDirectory(File, File, createDestDir), we MUST create two File objects before!
File from = new File(fromDir);
File to = new File(toDir);
/*returns void, throws exceptions:
NullPointerException - if source or destination is null
FileExistsException - if the directory or file exists in the destination directory
IOException - if source or destination is invalid or if an IO error occurs moving the file*/
FileUtils.moveToDirectory(from, to, false);
}
Luckily there's the Apache Commons IO library. Using that, you would do something like:
import java.util.*;
import java.io.*;
import org.apache.commons.io.FileUtils;
public void move(String fromDir, String toDir){
//moveToDirectory(File, File, createDestDir), we MUST create two File objects before!
File from = new File(fromDir);
File to = new File(toDir);
/*returns void, throws exceptions:
NullPointerException - if source or destination is null
FileExistsException - if the directory or file exists in the destination directory
IOException - if source or destination is invalid or if an IO error occurs moving the file*/
FileUtils.moveToDirectory(from, to, false);
}
[DOS] touch equivalent command to create a file
How do you create a file from DOS?
Usually
some_command >> filename
works but what if you just need to create an empty file? Well there's no single command like touch you can use, but a simple way to achieving the same result is by combining type and the output redirection.
type, will show you the input file contents. type nul will return successfully without showing any output. Now it's just a matter of adding redirection:
type nul >> filename
and you'll have created and empty file.
Usually
some_command >> filename
works but what if you just need to create an empty file? Well there's no single command like touch you can use, but a simple way to achieving the same result is by combining type and the output redirection.
type, will show you the input file contents. type nul will return successfully without showing any output. Now it's just a matter of adding redirection:
type nul >> filename
and you'll have created and empty file.
[Remmina] Cannot connect to RDP server
When using Remmina you may sometimes encounter the generic 'Cannot connect to RDP server' error even if all settings are correct, and usually there's no other debug information available.
What has helped me most of the times is simply deleting the server entry line from ~/.freerdp/known_hosts then trying to reconnect. You should be prompted to accept the server key again and eventually it should connect.
What has helped me most of the times is simply deleting the server entry line from ~/.freerdp/known_hosts then trying to reconnect. You should be prompted to accept the server key again and eventually it should connect.
[Oracle] Find DB components versions
Here's a simple query to quickly find the currently installed versions of Oracle DB's components:
SELECT * FROM v$version;
SELECT * FROM v$version;