PDA

View Full Version : فشرده کردن فایل



ramin_B
جمعه 29 اسفند 1393, 13:03 عصر
سلام

میخوام یه فایل رو zip کنم از کد زیر استفاده کردم اما خطا acces dinaied میدهد به نظر شما اشکال کجاست



public void getZipFiles(String zipFile, String destFolder) throws IOException {
BufferedOutputStream dest = null;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));




ZipEntry entry;
while (( entry = zis.getNextEntry() ) != null) {
System.out.println( "Extracting: " + entry.getName() );
int count;
byte data[] = new byte[1000];


if (entry.isDirectory()) {
new File( destFolder + "/" + entry.getName() ).mkdirs();
continue;
} else {
int di = entry.getName().lastIndexOf( '/' );
if (di != -1) {
new File( destFolder + "/" + entry.getName().substring( 0, di ) ).mkdirs();
}
}


FileOutputStream fos = new FileOutputStream( destFolder + "/" + entry.getName() );
dest = new BufferedOutputStream( fos );


while (( count = zis.read( data ) ) != -1) {
dest.write( data, 0, count );
}


dest.flush();
dest.close();

ahmad.mo74
جمعه 29 اسفند 1393, 20:35 عصر
سلام

در واقع دارید فایل زیپ رو اکسترکت میکنید!

اررور Access Denied موقعی رخ میده که شما سعی میکنید آدرس یه فولدر (directory) رو به FileOutputStream یا ... بدید و باهاش به عنوان یه فایل برخورد کنی.
یعنی خطوط 26 تا 36 باید بره داخل else.

من یه نمونه کامل نوشتم امیدوارم به دردت بخوره :


import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


/**
* @author avb
*/
public class Zipper {


public static boolean unzip(String fileName, String output) throws IOException {
File zipFile = new File(fileName);
if (!zipFile.exists() || zipFile.isDirectory()) {
return false;
}
if (output != null) {
createDirectories(output);
} else {
output = zipFile.getParent();
}
try (ZipInputStream zipInputStream = new ZipInputStream(readFully(zipFile))) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
File file = new File(output + File.separator + entry.getName());
createDirectories(file.getParent());
if (!entry.isDirectory()) {
save(Channels.newChannel(zipInputStream), file);
}
}
}
return true;
}


public static boolean unzip(String fileName) throws IOException {
return unzip(fileName, null);
}


public static boolean unzipToFolder(String fileName) throws IOException {
return unzip(fileName, fileName.substring(0, fileName.lastIndexOf('.')));
}


private static InputStream readFully(File file) throws IOException {
try (FileChannel in = FileChannel.open(Paths.get(file.getCanonicalPath() ));
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ByteBuffer buffer = ByteBuffer.allocate(64 * 1024);
in.force(false);
while (in.read(buffer) >= 0 || buffer.position() > 0) {
buffer.flip();
out.write(buffer.array());
buffer.clear();
}
return new ByteArrayInputStream(out.toByteArray());
}
}


private static void createDirectories(String fileName) throws IOException {
Path path = Paths.get(fileName);
if (Files.notExists(path)) {
Files.createDirectories(path);
}
}


private static void save(ReadableByteChannel in, File file) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(file);
FileChannel out = fileOutputStream.getChannel()) {
long pos = 0, count = 64 * 1024, transferred;
while ((transferred = out.transferFrom(in, pos, count)) > 0) {
pos += transferred;
}
}
}


public static void main(String[] args) throws IOException {
String fileName = "D:/Some.zip";
System.out.println(unzip(fileName));
System.out.println(unzip(fileName, "D:/output"));
System.out.println(unzipToFolder(fileName));
}


}