我可以压缩特定文件夹中包含的文件。这是我使用过的代码:
public class Compress {
private static final int BUFFER = 2048;
private String[] _files;
private String _zipFile;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
for(int i=0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
我在另一个类中以这种方式调用这个类:
String[] files = {mainFolderPath+"/text1.txt", mainFolderPath+ "/text2.txt", mainFolderPath +"/NewFolder"};
Compress compress = new Compress(files, sourceFile.getAbsolutePath());
compress.zip();
在运行应用程序时,我收到了IOException。
您能告诉我如何压缩包含另一个文本文件的“NewFolder”以及文本文件“text1.txt”和“text2.txt”吗?
谢谢。
答案 0 :(得分:0)
它对我有用!!!!!!!!在我需要的所有项目中......
public String[] _files;
public String _zipFile = "/mnt/sdcard/123.zip";
/////zIIIiiiiPPPPPer
public class Compress {
private static final int BUFFER = 2048;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
String path = "/mnt/sdcard/out";
File fileDir = new File( path );
if(!fileDir.exists() || !fileDir.isDirectory())
{
return;
}
String[] _files = fileDir.list();
for ( int i = 0 ; i < _files.length ; i++ )
{
_files[i] = path + "/"+ _files[i];
}
byte data[] = new byte[BUFFER];
for(int i=0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
称之为JuSt就像......一样......
Compress cs = new Compress(_files, _zipFile);
答案 1 :(得分:0)
我得到了我的问题的解决方案,即;仅压缩包含文件夹和某些文本文件的特定文件夹的内容。这是代码:
public class Compress {
private static final int BUFFER = 2048;
private String[] _files;
private String _zipFile;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
byte data[] = new byte[BUFFER];
for (int i = 0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
String name = _files[i];
File f = new File( _files[i]);
if (f.isDirectory()) {
name = name.endsWith("/") ? name : name + "/";
for (String file : f.list()) {
System.out.println(" checking " + file);
System.out
.println("The folder name is: " + f.getName());
out.putNextEntry(new ZipEntry(f.getName() + "/" + file));
FileInputStream fi = new FileInputStream( _files[i]
+ "/" + file);
origin = new BufferedInputStream(fi, BUFFER);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
System.out.println(" checking folder" + name);
} else {
FileInputStream fi = new FileInputStream( _files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry( _files[i].substring( _files[i]
.lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}