Skip to content

Commit

Permalink
Support view zip via gbk encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
cweijan committed Mar 29, 2024
1 parent f233eb1 commit c5bc139
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/bundle/adm-zip/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ declare namespace AdmZip {
}

interface InitOptions {
encoding: string;
/* If true it disables files sorting */
noSort: boolean;
/* Read entries during load (initial loading may be slower) */
Expand Down
7 changes: 5 additions & 2 deletions src/bundle/adm-zip/zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var Utils = require("./util"),
Methods = require("./methods");
import iconv from 'iconv-lite';

module.exports = function (/*Buffer*/ input) {
module.exports = function (/*Buffer*/ input, /** object */ options) {
var _entryHeader = new Headers.EntryHeader(),
_entryName = Buffer.alloc(0),
_comment = Buffer.alloc(0),
Expand Down Expand Up @@ -199,7 +199,10 @@ module.exports = function (/*Buffer*/ input) {
return {
get entryName() {
const buffer = _entryName;
return buffer.toString('utf-8')
if (options?.encoding == 'gbk') {
return iconv.decode(buffer, 'gbk');
}
return buffer.toString(options?.encoding || 'utf-8')
},
get rawEntryName() {
return _entryName;
Expand Down
4 changes: 2 additions & 2 deletions src/bundle/adm-zip/zipFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {

for (let i = 0; i < totalEntries; i++) {
let tmp = index;
const entry = new ZipEntry(inBuffer);
const entry = new ZipEntry(inBuffer, options);

entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));
entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
Expand All @@ -46,7 +46,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
var index = mainHeader.offset; // offset of first CEN header
for (var i = 0; i < entryList.length; i++) {
var tmp = index,
entry = new ZipEntry(inBuffer);
entry = new ZipEntry(inBuffer, options);
entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));

entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
Expand Down
14 changes: 13 additions & 1 deletion src/provider/handlers/zipHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ export async function handleZip(uri: Uri, handler: Handler) {
handler.on('init', async () => {
const data = (await workspace.fs.readFile(uri)) as Buffer
const basePath = `${tmpdir()}/officeZip.${new Date().getTime()}`;
const { zip, files, folderMap, fileMap } = parseZipAsTree(data)
let { zip, files, folderMap, fileMap } = parseZipAsTree(data)
handler.emit('data', {
files, folderMap,
fileName: basename(uri.fsPath)
})

handler.on('changeEncoding', async (encoding) => {
const info = parseZipAsTree(data, { encoding });
zip = info.zip;
files = info.files;
folderMap = info.folderMap;
fileMap = info.fileMap;
handler.emit('data', {
files, folderMap,
fileName: basename(uri.fsPath)
})
}).on('openPath', async info => {
const { entryName, isDirectory } = info
if (isDirectory) {
Expand Down
33 changes: 24 additions & 9 deletions src/react/view/compress/components/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { FileAddOutlined, FileDoneOutlined } from '@ant-design/icons';
import { Button } from "antd";
import { Button, Flex, Select } from "antd";
import { handler } from "../../../util/vscode";

export default function Toolbar({ currentDir }) {
return (
<div style={{ padding: '5px', backgroundColor: 'white' }}>
<Button type="primary" size='middle' style={{ marginRight: '10px' }} icon={<FileDoneOutlined />} onClick={() => handler.emit('autoExtract')}>
Extract
</Button>
<Button size='middle' icon={<FileAddOutlined />} onClick={() => { handler.emit('addFile', currentDir) }} >
Add
</Button>
</div>
<Flex justify='space-between' style={{ padding: '5px', backgroundColor: 'white' }}>
<div>
<Button type="primary" size='middle' style={{ marginRight: '10px' }} icon={<FileDoneOutlined />} onClick={() => handler.emit('autoExtract')}>
Extract
</Button>
<Button size='middle' icon={<FileAddOutlined />} onClick={() => { handler.emit('addFile', currentDir) }} >
Add
</Button>
</div>
<div>
Encoding:
<Select
defaultValue="utf8"
size='middle'
style={{ width: 120, marginLeft: '15px', marginRight: '20px' }}
onChange={(value) => handler.emit('changeEncoding', value)}
options={[
{ value: 'gbk', label: 'GBK' },
{ value: 'utf8', label: 'UTF-8' },
]}
/>
</div>
</Flex>
)
}
4 changes: 2 additions & 2 deletions src/service/zip/zipUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ interface ZipParseResult {
folderMap: { [fullPath: string]: ZipEntry }
}

export function parseZipAsTree(zipData: Buffer): ZipParseResult {
const zip = new AdmZip(zipData);
export function parseZipAsTree(zipData: Buffer, option?: Partial<AdmZip.InitOptions>): ZipParseResult {
const zip = new AdmZip(zipData, option);
const zipEntries = zip.getEntries().filter(e => e.name);

let files: ZipEntry[] = []
Expand Down

0 comments on commit c5bc139

Please sign in to comment.