Unzip saz files java - java

I was looking through java documentation and there doesn't seem to be a way to specifically unzip saz files, which are session archives created by the network proxy Fiddler. Anyone have any idea of how to do it?

According this it is regular ZIP with specific extension for file name. Use java.util.zip.ZipFile.
About not having specific method for this extension - I think it kind of makes sense to not to have specific method for every possible extension.

This should do the trick
saz files are regular zip files
public static boolean unzipFiles(String srcDirectory, String srcFile, String destDirectory)
{
try
{
//first make sure that all the arguments are valid and not null
if(srcDirectory == null)
{
System.out.println(1);
return false;
}
if(srcFile == null)
{
System.out.println(2);
return false;
}
if(destDirectory == null)
{
System.out.println(3);
return false;
}
if(srcDirectory.equals(""))
{
System.out.println(4);
return false;
}
if(srcFile.equals(""))
{
System.out.println(5);
return false;
}
if(destDirectory.equals(""))
{
System.out.println(6);
return false;
}
//now make sure that these directories exist
File sourceDirectory = new File(srcDirectory);
File sourceFile = new File(srcDirectory + File.separator + srcFile);
File destinationDirectory = new File(destDirectory);
if(!sourceDirectory.exists())
{
System.out.println(7);
return false;
}
if(!sourceFile.exists())
{
System.out.println(sourceFile);
return false;
}
if(!destinationDirectory.exists())
{
System.out.println(9);
return false;
}
//now start with unzip process
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(sourceFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null)
{
String outputFilename = destDirectory + File.separator + entry.getName();
System.out.println("Extracting file: " + entry.getName());
createDirIfNeeded(destDirectory, entry);
int count;
byte data[] = new byte[BUFFER_SIZE];
//write the file to the disk
FileOutputStream fos = new FileOutputStream(outputFilename);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while((count = zis.read(data, 0, BUFFER_SIZE)) != -1)
{
dest.write(data, 0, count);
}
//close the output streams
dest.flush();
dest.close();
}
//we are done with all the files
//close the zip file
zis.close();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}

This page, found by googling "saz files", says:
SAZ files are simply specially formatted .ZIP files. If you rename a
.SAZ file to .ZIP, you can open it for viewing using standard ZIP
viewing tools
So they're just zip files with a different extension. Unzip them exactly like you would unzip a zip file.

Related

How to decompress BZIP (not BZIP2) with Apache Commons

I have been working on a task to decompress from different types of file format such as "zip,tar,tbz,tgz". I am able to do for all except tbz because apache common compress library provides BZIP2 compressors. But I need to decompress a old BZIP not BZIP2. Is there any way to do it java. I have added the code I have done so far for extracting different tar file archives using apache commons library below.
public List<ArchiveFile> processTarFiles(String compressedFilePath, String fileType) throws IOException {
List<ArchiveFile> extractedFileList = null;
TarArchiveInputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream dest = null;
try {
if(fileType.equalsIgnoreCase("tar"))
{
is = new TarArchiveInputStream(new FileInputStream(new File(compressedFilePath)));
}
else if(fileType.equalsIgnoreCase("tbz")||fileType.equalsIgnoreCase("bz"))
{
is = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(new File(compressedFilePath))));
}
else if(fileType.equalsIgnoreCase("tgz")||fileType.equalsIgnoreCase("gz"))
{
is = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(new File(compressedFilePath))));
}
TarArchiveEntry entry = is.getNextTarEntry();
extractedFileList = new ArrayList<>();
while (entry != null) {
// grab a zip file entry
String currentEntry = entry.getName();
if (!entry.isDirectory()) {
File destFile = new File(Constants.DEFAULT_ZIPOUTPUTPATH, currentEntry);
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
ArchiveFile archiveFile = new ArchiveFile();
int currentByte;
// establish buffer for writing file
byte data[] = new byte[(int) entry.getSize()];
// write the current file to disk
fos = new FileOutputStream(destFile);
dest = new BufferedOutputStream(fos, (int) entry.getSize());
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, (int) entry.getSize())) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
archiveFile.setExtractedFilePath(destFile.getAbsolutePath());
archiveFile.setFormat(destFile.getName().split("\\.")[1]);
extractedFileList.add(archiveFile);
entry = is.getNextTarEntry();
} else {
new File(Constants.DEFAULT_ZIPOUTPUTPATH, currentEntry).mkdirs();
entry = is.getNextTarEntry();
}
}
} catch (IOException e) {
System.out.println(("ERROR: " + e.getMessage()));
} catch (Exception e) {
System.out.println(("ERROR: " + e.getMessage()));
} finally {
is.close();
dest.flush();
dest.close();
}
return extractedFileList;
}
The original Bzip was supposedly using a patented algorithm so Bzip2 was born using algorithms and techniques that were not patented.
That might be the reason why it's no longer in widespread use and open source libraries ignore it.
There's some C code for decompressing Bzip files shown here (gist.github.com mirror).
You might want to read and rewrite that in Java.

Zip file not deleted even if I am getting its correct name and path

I am trying to delete a zip file after unziping. but I am not able to delete it:
if (file.getName().contains(".zip")) {
System.out.println(file.getAbsolutePath()); // I am getting the correct path
file.delete();
System.out.println(file.getName()); // I am getting the correct name Script-1.zip
}
This is the full code
public class Zip4 {
public static void main(String[] args) {
File[] files = new File(args[0]).listFiles();
for(File file : files)
// System.out.println(file.getName());
//if(file.getName().contains("1400") && file.getName().contains(".zip"))
extractFolder(args[0] + file.getName(), args[1]);
DeleteFiles();
// for(File file : files)
// System.out.println("File:C:/1/"+ file.getName());
// extractFolder(args[0]+file.getName(),args[1]);
}
private static void DeleteFiles()
{
File f = null;
File[] paths;
f = new File("D:/Copyof");
paths = f.listFiles();
for(File path:paths)
{
// prints file and directory paths
if(path.getName().contains("J14_0_0RC") || path.getName().contains(".zip") || path.getName().contains(".log"))
{
//System.out.println(path);
path.delete();
}
}
}
private static void extractFolder(String zipFile,String extractFolder)
{
try
{
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = extractFolder;
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
//destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
fos.flush();
fos.close();
is.close();
}
}
if(file.getName().contains(".zip"))
{
System.out.println(file.getAbsolutePath());
file.delete();
System.out.println(file.getName());
}
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}
}
ZipFile is a closeable resource. So either close() it once you're done in a finally block or create it with try-with-resources (since java7):
try(ZipFile zip = new ZipFile(file)){
//unzip here
}
file.delete();
Apart from this, you should revisit this block
dest.flush();
dest.close();
fos.flush();
fos.close();
is.close();
which is quite prone to resource leaks. If one of the upper calls fails, all subsequent calls are not invoked, resulting in unclosed resources and resource leakage.
So best would be to use try-with-resources here, too.
try(BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
//write the data
} //all streams are closed implicitly here
Or use an existing tool for that, for example Apache Commons IO IOUtil.closeQuietly(resource) or embedd every single call into
if(resource != null) {
try{
resource.close();
} catch(IOException e){
//omit
}
}
You could also omit the call to flush() which is done implicitly when closing the resource.

Reading gzip files inside gzip file using Java

Using Java I have to read text files which are inside gz file which is in another .tar.gz
gz_ltm_logs.tar.gz is the filename. It then has files ltm.1.gz, ltm.2.gz inside it and then these files have text files in them.
I wanted to do it using java.util.zip.* only but if it is impossible then I can look at other libraries.
I thought I will be able to do it using java.util.zip. But doesn't seem straightforward
Here's some code to give you an idea. This method will try to extract a given tar.gz file to outputFolder.
public static void extract(File input, File outputFolder) throws IOException {
byte[] buffer = new byte[1024];
GZIPInputStream gzipFile = new GZIPInputStream(new FileInputStream(input));
ByteOutputStream tarStream = new ByteOutputStream();
int gzipLengthRead;
while ((gzipLengthRead = gzipFile.read(buffer)) > 0){
tarStream.write(buffer, 0, gzipLengthRead);
}
gzipFile.close();
org.apache.tools.tar.TarInputStream tarFile = null;
// files inside the tar
OutputStream out = null;
try {
tarFile = new org.apache.tools.tar.TarInputStream(tarStream.newInputStream());
tarStream.close();
TarEntry entry = null;
while ((entry = tarFile.getNextEntry()) != null) {
String outFilename = entry.getName();
if (entry.isDirectory()) {
File directory = new File(outputFolder, outFilename);
directory.mkdirs();
} else {
File outputFile = new File(outputFolder, outFilename);
File outputDirectory = outputFile.getParentFile();
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
out = new FileOutputStream(outputFile);
// Transfer bytes from the tarFile to the output file
int innerLen;
while ((innerLen = tarFile.read(buffer)) > 0) {
out.write(buffer, 0, innerLen);
}
out.close();
}
}
} finally {
if (tarFile != null) {
tarFile.close();
}
if (out != null) {
out.close();
}
}
}

what does getNextEntry() do?

I have checked java api document, it says getNextEntry() Reads the next ZIP file entry and positions the stream at the beginning of the entry data.
what does it mean "reads the NEXT zip file"? why the "NEXT" ?
I have this piece of code, what's the point of this line ze = zin.getNextEntry()?
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
It reads the next entry within the zip file.
A zip file logically contains main other files - so foo.zip can contain files a.txt and b.txt. The getNextEntry() moves you on to the next file within the archive.
(I've never been particularly keen on the way that ZipInputStream is modeled using inheritance from InputStream, but that's a different matter.)

Unable to extract folders from zip file using ZipEntry in windows

java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(new BufferedInputStream(is));
java.util.zip.ZipEntry entry;
new File(outdir+ File.separator+"changelog").delete();
new File(outdir+ File.separator+"media").delete();
try {
while ((entry = zis.getNextEntry()) != null) {
File of = new File(outdir + File.separator + entry.getName());
if (entry.isDirectory()) {
of.mkdirs();
continue;
} else {
File xx = new File(of.getParent());
if (!xx.exists()) {
Stack<String> todo = new Stack<String>();
do {
todo.push(xx.getAbsolutePath());
xx = new File(xx.getParent());
} while (!xx.exists());
while (todo.size() > 0) {
xx = new File(todo.pop());
if (!xx.exists()) {
xx.mkdirs();
}
}
}
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(of), buffer.length);
cpio(new BufferedInputStream(zis), bos, "unzip:" + entry.getName());
bos.flush();
bos.close();
}
} catch (IllegalArgumentException e) {
// problem with chars in entry name likely
}catch(Exception e){
System.out.println(e+"Srikanth");
}
zis.close();
}
entry.isDirectory() always returing false so it is creating files instead of directories. What is the problem?
ZipEntry from ZipInputStream representing empty directory at end of file with \ , directory with elements with /
So that entry.isDirectory() not working with empty directory.
Where as ZipEntry from ZipFile working fine. I think there is difference between ZipInputStream and ZipEntry behavior.
isDirectory is not working at all with files zipped with Windows standard option "sent to/ zip files"
The format of the zip is different than format generated with tools like 7zip or Winzip. (Nice to have a standard archive compression :D)

Categories

Resources