473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem in Unzipping the zip files

Hi,

I have created a program to unzip the file. It is unzipping the zip files when put in directory i.e folder inside folder but when i create a zip file without putting in a folder and try to unzip it. It unzips it but put the zipped file outside the folder created in the name of the zip file. I want it to be inside it.

Also when i do beyond compare of the two files i.e the zipped file and original file its shows difference.

What should i do

I have pasted the code below:


//Unzip the zip files to the folder with their name

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


// Getting the path to find the type of files
public class zipper implements FilenameFilter{
String ext;

public zipper(String ext)
{
this.ext="." + ext;
}

public boolean accept(File dir,String name)
{
return name.endsWith(ext);
}

public static void main(String args[]) throws IOException
{
try
{
String dir = "D:/";

File f2 = new File(dir);

FilenameFilter fn= new zipper("zip");
String ss[]=f2.list(fn);


for ( int j = 0; j < ss.length; j++)
{
System.out.println(" Extracting ...." + ss[j]) ;
String zipFile = dir + ss[j];

ZipFile zf = new ZipFile(zipFile);
Enumeration entries = zf.entries();

String directoryName = zf.getName();
directoryName = directoryName.substring(0, (directoryName.indexOf(":" + File.separator) + 2));

String folderName = zf.getName();
folderName = folderName.substring(0, folderName.lastIndexOf("."));
File file1 = new File(folderName);
file1.mkdir();

while (entries.hasMoreElements())
{
ZipEntry ze = (ZipEntry) entries.nextElement();
String path = directoryName + ze.getName() ;

//System.out.println(" : " + path);
if (ze.getName().endsWith("/"))
{
File file = new File(path);
file.mkdir();
//continue;
break;
}

BufferedReader bReader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
StringBuffer fileBuffer = new StringBuffer(" ");
String line ;

while ((line = bReader.readLine()) != null)
{
fileBuffer.append(line);
fileBuffer.append("\r\n");
//line = line + "\r\n";
//byte[] b = new byte[line.length()];
//b =line.getBytes();
//out.write(b);

}

String fileData = fileBuffer.toString();
File f1 = new File(path);
f1.createNewFile();
FileOutputStream out = new FileOutputStream(path);
//FileOutputStream out = new FileOutputStream(new File(folderName + "/" + ze.getName()));
long size = ze.getSize();

byte[] data1 = new byte[fileData.length()];

for (int i = 0; i < fileData.length(); i++)
{
data1[i] = (byte) fileData.charAt(i);
}
out.write(data1);
out.close();
//bReader.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}


Thanks in Advance

Avinash
Sep 11 '07 #1
0 2043

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Terry | last post by:
I have created a switchboard, created using Access 2000. If I put the mouse anywhere on the form, outside of the checkboxes and scroll the wheel backwards, I get the following error. Run-Time...
0
by: Owen Jenkins | last post by:
I've been using Dev's fantastic InetTransferLib to upload and download files via ftp. Now I'm using the http procedures to have my application download zip files from my website. Although the...
1
by: Owen Jenkins | last post by:
Reposting this message from last week since I have not had a reply so far. Hopefully someone will have an idea? --- I've been using Dev's fantastic InetTransferLib to upload and download files...
7
by: thewhoracle | last post by:
I have a C# application that essentially builds batch and text files and runs them from the command line, but i need to unzip something from the command line. What i need is either an unzipping...
1
by: batista | last post by:
Hi there, i have a web service which is using a dll written in vc7.This dll in turn is accessing a vc6 com dll through interop. Now the problem is that i have a function in vc6 that takes as...
0
by: stjulian | last post by:
On Windows Server 2003, IIS 6 I am looking for an unzipping component. I have a series of files that are available zipped at a considerable decrease in size. And before ASPINET fails on a script...
1
by: kss | last post by:
Hey all, I am new to python and I need to unzip a zip file which is password protected. I tried using zipfile class. Aparently, when i try doing a zip.read(filename), it throws an exception stating...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
1
by: deepakd | last post by:
Hi I'm using wxZipInputStream for unzipping a zipped ".app" file in MAC OS.After unzipping i am not able to run the app.When i manually copy the unix executable to app packages, I am able to run...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.